Mun Gwan-gyeong | 6f01c55 | 2016-06-27 05:34:44 +0900 | [diff] [blame] | 1 | /* |
Tony-LunarG | 495604b | 2019-06-13 15:32:05 -0600 | [diff] [blame] | 2 | * Copyright (c) 2015-2019 The Khronos Group Inc. |
| 3 | * Copyright (c) 2015-2019 Valve Corporation |
| 4 | * Copyright (c) 2015-2019 LunarG, Inc. |
Ian Elliott | d940ca2 | 2017-03-22 08:31:27 -0600 | [diff] [blame] | 5 | * |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | * |
| 18 | * Author: Chia-I Wu <olv@lunarg.com> |
| 19 | * Author: Courtney Goeltzenleuchter <courtney@LunarG.com> |
| 20 | * Author: Ian Elliott <ian@LunarG.com> |
| 21 | * Author: Ian Elliott <ianelliott@google.com> |
| 22 | * Author: Jon Ashburn <jon@lunarg.com> |
| 23 | * Author: Gwan-gyeong Mun <elongbug@gmail.com> |
| 24 | * Author: Tony Barbour <tony@LunarG.com> |
| 25 | * Author: Bill Hollings <bill.hollings@brenwill.com> |
| 26 | */ |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 27 | |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 28 | #define _GNU_SOURCE |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 29 | #include <stdio.h> |
Ian Elliott | d940ca2 | 2017-03-22 08:31:27 -0600 | [diff] [blame] | 30 | #include <stdarg.h> |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 31 | #include <stdlib.h> |
| 32 | #include <string.h> |
| 33 | #include <stdbool.h> |
| 34 | #include <assert.h> |
Courtney Goeltzenleuchter | af1951c | 2015-12-01 14:11:38 -0700 | [diff] [blame] | 35 | #include <signal.h> |
Mun Gwan-gyeong | 2253389 | 2016-08-20 14:46:22 +0900 | [diff] [blame] | 36 | #if defined(VK_USE_PLATFORM_XLIB_KHR) || defined(VK_USE_PLATFORM_XCB_KHR) |
Tony Barbour | 2593b18 | 2016-04-19 10:57:58 -0600 | [diff] [blame] | 37 | #include <X11/Xutil.h> |
Joey Bzdek | 15eb070 | 2017-06-07 09:40:36 -0600 | [diff] [blame] | 38 | #elif defined(VK_USE_PLATFORM_WAYLAND_KHR) |
| 39 | #include <linux/input.h> |
Tony Barbour | 2593b18 | 2016-04-19 10:57:58 -0600 | [diff] [blame] | 40 | #endif |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 41 | |
Ian Elliott | 639ca47 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 42 | #ifdef _WIN32 |
Biswapriyo Nath | ffe404f | 2020-08-25 01:47:41 +0530 | [diff] [blame] | 43 | #ifdef _MSC_VER |
Ian Elliott | 639ca47 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 44 | #pragma comment(linker, "/subsystem:windows") |
Biswapriyo Nath | c472fa2 | 2020-08-25 23:49:34 +0530 | [diff] [blame] | 45 | #endif // MSVC |
Ian Elliott | 639ca47 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 46 | #define APP_NAME_STR_LEN 80 |
Mark Lobodzinski | 85dbd82 | 2017-01-26 13:34:13 -0700 | [diff] [blame] | 47 | #endif // _WIN32 |
Ian Elliott | 639ca47 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 48 | |
Cody Northrop | 8707a6e | 2016-04-26 19:59:19 -0600 | [diff] [blame] | 49 | #ifdef ANDROID |
| 50 | #include "vulkan_wrapper.h" |
| 51 | #else |
David Pinedo | c5193c1 | 2015-11-06 12:54:48 -0700 | [diff] [blame] | 52 | #include <vulkan/vulkan.h> |
Cody Northrop | 8707a6e | 2016-04-26 19:59:19 -0600 | [diff] [blame] | 53 | #endif |
Ian Elliott | b08e0d9 | 2015-11-20 11:55:46 -0700 | [diff] [blame] | 54 | |
David Pinedo | 541526f | 2015-11-24 09:00:24 -0700 | [diff] [blame] | 55 | #include <vulkan/vk_sdk_platform.h> |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 56 | #include "linmath.h" |
Mark Lobodzinski | 0edf138 | 2018-04-10 15:40:04 -0600 | [diff] [blame] | 57 | #include "object_type_string_helper.h" |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 58 | |
Ian Elliott | d940ca2 | 2017-03-22 08:31:27 -0600 | [diff] [blame] | 59 | #include "gettime.h" |
| 60 | #include "inttypes.h" |
| 61 | #define MILLION 1000000L |
| 62 | #define BILLION 1000000000L |
| 63 | |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 64 | #define DEMO_TEXTURE_COUNT 1 |
Lenny Komow | ffe446c | 2018-11-19 17:08:04 -0700 | [diff] [blame] | 65 | #define APP_SHORT_NAME "vkcube" |
| 66 | #define APP_LONG_NAME "Vulkan Cube" |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 67 | |
Tony Barbour | 66a56c3 | 2016-09-21 13:10:56 -0600 | [diff] [blame] | 68 | // Allow a maximum of two outstanding presentation operations. |
| 69 | #define FRAME_LAG 2 |
| 70 | |
Courtney Goeltzenleuchter | 886f76c | 2015-07-06 17:46:11 -0600 | [diff] [blame] | 71 | #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) |
| 72 | |
Tony Barbour | fdc2d35 | 2015-04-22 09:02:32 -0600 | [diff] [blame] | 73 | #if defined(NDEBUG) && defined(__GNUC__) |
| 74 | #define U_ASSERT_ONLY __attribute__((unused)) |
| 75 | #else |
| 76 | #define U_ASSERT_ONLY |
| 77 | #endif |
| 78 | |
Mun Gwan-gyeong | 6f01c55 | 2016-06-27 05:34:44 +0900 | [diff] [blame] | 79 | #if defined(__GNUC__) |
| 80 | #define UNUSED __attribute__((unused)) |
| 81 | #else |
| 82 | #define UNUSED |
| 83 | #endif |
| 84 | |
Ian Elliott | 0726413 | 2015-04-28 11:35:02 -0600 | [diff] [blame] | 85 | #ifdef _WIN32 |
Tony Barbour | 602ca4f | 2016-09-12 14:02:43 -0600 | [diff] [blame] | 86 | bool in_callback = false; |
Mark Lobodzinski | 85dbd82 | 2017-01-26 13:34:13 -0700 | [diff] [blame] | 87 | #define ERR_EXIT(err_msg, err_class) \ |
| 88 | do { \ |
| 89 | if (!demo->suppress_popups) MessageBox(NULL, err_msg, err_class, MB_OK); \ |
| 90 | exit(1); \ |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 91 | } while (0) |
Ian Elliott | d940ca2 | 2017-03-22 08:31:27 -0600 | [diff] [blame] | 92 | void DbgMsg(char *fmt, ...) { |
| 93 | va_list va; |
| 94 | va_start(va, fmt); |
Mike Weiblen | e9847ff | 2019-06-27 15:18:32 -0600 | [diff] [blame] | 95 | vprintf(fmt, va); |
Ian Elliott | d940ca2 | 2017-03-22 08:31:27 -0600 | [diff] [blame] | 96 | va_end(va); |
Mike Weiblen | e9847ff | 2019-06-27 15:18:32 -0600 | [diff] [blame] | 97 | fflush(stdout); |
Ian Elliott | d940ca2 | 2017-03-22 08:31:27 -0600 | [diff] [blame] | 98 | } |
Ian Elliott | 0726413 | 2015-04-28 11:35:02 -0600 | [diff] [blame] | 99 | |
Michael Lentine | c6cde4b | 2016-04-18 13:20:45 -0500 | [diff] [blame] | 100 | #elif defined __ANDROID__ |
| 101 | #include <android/log.h> |
Jeremy Kniager | 979b531 | 2019-11-22 14:55:57 -0700 | [diff] [blame] | 102 | #define ERR_EXIT(err_msg, err_class) \ |
| 103 | do { \ |
Lenny Komow | ffe446c | 2018-11-19 17:08:04 -0700 | [diff] [blame] | 104 | ((void)__android_log_print(ANDROID_LOG_INFO, "Vulkan Cube", err_msg)); \ |
Jeremy Kniager | 979b531 | 2019-11-22 14:55:57 -0700 | [diff] [blame] | 105 | exit(1); \ |
Michael Lentine | c6cde4b | 2016-04-18 13:20:45 -0500 | [diff] [blame] | 106 | } while (0) |
Ian Elliott | d940ca2 | 2017-03-22 08:31:27 -0600 | [diff] [blame] | 107 | #ifdef VARARGS_WORKS_ON_ANDROID |
| 108 | void DbgMsg(const char *fmt, ...) { |
| 109 | va_list va; |
| 110 | va_start(va, fmt); |
Lenny Komow | ffe446c | 2018-11-19 17:08:04 -0700 | [diff] [blame] | 111 | __android_log_print(ANDROID_LOG_INFO, "Vulkan Cube", fmt, va); |
Ian Elliott | d940ca2 | 2017-03-22 08:31:27 -0600 | [diff] [blame] | 112 | va_end(va); |
| 113 | } |
| 114 | #else // VARARGS_WORKS_ON_ANDROID |
Jeremy Kniager | 979b531 | 2019-11-22 14:55:57 -0700 | [diff] [blame] | 115 | #define DbgMsg(fmt, ...) \ |
| 116 | do { \ |
Lenny Komow | ffe446c | 2018-11-19 17:08:04 -0700 | [diff] [blame] | 117 | ((void)__android_log_print(ANDROID_LOG_INFO, "Vulkan Cube", fmt, ##__VA_ARGS__)); \ |
Ian Elliott | d940ca2 | 2017-03-22 08:31:27 -0600 | [diff] [blame] | 118 | } while (0) |
| 119 | #endif // VARARGS_WORKS_ON_ANDROID |
Michael Lentine | c6cde4b | 2016-04-18 13:20:45 -0500 | [diff] [blame] | 120 | #else |
Mark Lobodzinski | 85dbd82 | 2017-01-26 13:34:13 -0700 | [diff] [blame] | 121 | #define ERR_EXIT(err_msg, err_class) \ |
| 122 | do { \ |
Robert Morell | 4ccc652 | 2017-02-01 14:51:00 -0800 | [diff] [blame] | 123 | printf("%s\n", err_msg); \ |
Mark Lobodzinski | 85dbd82 | 2017-01-26 13:34:13 -0700 | [diff] [blame] | 124 | fflush(stdout); \ |
| 125 | exit(1); \ |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 126 | } while (0) |
Ian Elliott | d940ca2 | 2017-03-22 08:31:27 -0600 | [diff] [blame] | 127 | void DbgMsg(char *fmt, ...) { |
| 128 | va_list va; |
| 129 | va_start(va, fmt); |
Mike Weiblen | e9847ff | 2019-06-27 15:18:32 -0600 | [diff] [blame] | 130 | vprintf(fmt, va); |
Ian Elliott | d940ca2 | 2017-03-22 08:31:27 -0600 | [diff] [blame] | 131 | va_end(va); |
Mike Weiblen | e9847ff | 2019-06-27 15:18:32 -0600 | [diff] [blame] | 132 | fflush(stdout); |
Ian Elliott | d940ca2 | 2017-03-22 08:31:27 -0600 | [diff] [blame] | 133 | } |
Michael Lentine | c6cde4b | 2016-04-18 13:20:45 -0500 | [diff] [blame] | 134 | #endif |
Ian Elliott | 0726413 | 2015-04-28 11:35:02 -0600 | [diff] [blame] | 135 | |
Mark Lobodzinski | 85dbd82 | 2017-01-26 13:34:13 -0700 | [diff] [blame] | 136 | #define GET_INSTANCE_PROC_ADDR(inst, entrypoint) \ |
| 137 | { \ |
| 138 | demo->fp##entrypoint = (PFN_vk##entrypoint)vkGetInstanceProcAddr(inst, "vk" #entrypoint); \ |
| 139 | if (demo->fp##entrypoint == NULL) { \ |
| 140 | ERR_EXIT("vkGetInstanceProcAddr failed to find vk" #entrypoint, "vkGetInstanceProcAddr Failure"); \ |
| 141 | } \ |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 142 | } |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 143 | |
Jon Ashburn | 7d8342c | 2015-10-08 15:58:23 -0600 | [diff] [blame] | 144 | static PFN_vkGetDeviceProcAddr g_gdpa = NULL; |
| 145 | |
Mark Lobodzinski | 85dbd82 | 2017-01-26 13:34:13 -0700 | [diff] [blame] | 146 | #define GET_DEVICE_PROC_ADDR(dev, entrypoint) \ |
| 147 | { \ |
| 148 | if (!g_gdpa) g_gdpa = (PFN_vkGetDeviceProcAddr)vkGetInstanceProcAddr(demo->inst, "vkGetDeviceProcAddr"); \ |
| 149 | demo->fp##entrypoint = (PFN_vk##entrypoint)g_gdpa(dev, "vk" #entrypoint); \ |
| 150 | if (demo->fp##entrypoint == NULL) { \ |
| 151 | ERR_EXIT("vkGetDeviceProcAddr failed to find vk" #entrypoint, "vkGetDeviceProcAddr Failure"); \ |
| 152 | } \ |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 153 | } |
Ian Elliott | 673898b | 2015-06-22 15:07:49 -0600 | [diff] [blame] | 154 | |
Courtney Goeltzenleuchter | 97db1c1 | 2014-10-30 15:14:16 -0600 | [diff] [blame] | 155 | /* |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 156 | * structure to track all objects related to a texture. |
| 157 | */ |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 158 | struct texture_object { |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 159 | VkSampler sampler; |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 160 | |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 161 | VkImage image; |
Tony-LunarG | bc9fc05 | 2018-09-21 13:47:06 -0600 | [diff] [blame] | 162 | VkBuffer buffer; |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 163 | VkImageLayout imageLayout; |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 164 | |
Chia-I Wu | a8fe78a | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 165 | VkMemoryAllocateInfo mem_alloc; |
Mark Lobodzinski | 2dcdfd7 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 166 | VkDeviceMemory mem; |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 167 | VkImageView view; |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 168 | int32_t tex_width, tex_height; |
| 169 | }; |
| 170 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 171 | static char *tex_files[] = {"lunarg.ppm"}; |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 172 | |
Karl Schultz | 0218c00 | 2016-03-22 17:06:13 -0600 | [diff] [blame] | 173 | static int validation_error = 0; |
| 174 | |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 175 | struct vktexcube_vs_uniform { |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 176 | // Must start with MVP |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 177 | float mvp[4][4]; |
| 178 | float position[12 * 3][4]; |
| 179 | float attr[12 * 3][4]; |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 180 | }; |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 181 | |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 182 | //-------------------------------------------------------------------------------------- |
| 183 | // Mesh and VertexFormat Data |
| 184 | //-------------------------------------------------------------------------------------- |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 185 | // clang-format off |
Mark Lobodzinski | c4fffc7 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 186 | static const float g_vertex_buffer_data[] = { |
Chia-I Wu | ae3b55d | 2015-04-22 14:56:17 +0800 | [diff] [blame] | 187 | -1.0f,-1.0f,-1.0f, // -X side |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 188 | -1.0f,-1.0f, 1.0f, |
| 189 | -1.0f, 1.0f, 1.0f, |
Chia-I Wu | ae3b55d | 2015-04-22 14:56:17 +0800 | [diff] [blame] | 190 | -1.0f, 1.0f, 1.0f, |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 191 | -1.0f, 1.0f,-1.0f, |
| 192 | -1.0f,-1.0f,-1.0f, |
| 193 | |
Chia-I Wu | ae3b55d | 2015-04-22 14:56:17 +0800 | [diff] [blame] | 194 | -1.0f,-1.0f,-1.0f, // -Z side |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 195 | 1.0f, 1.0f,-1.0f, |
| 196 | 1.0f,-1.0f,-1.0f, |
Chia-I Wu | ae3b55d | 2015-04-22 14:56:17 +0800 | [diff] [blame] | 197 | -1.0f,-1.0f,-1.0f, |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 198 | -1.0f, 1.0f,-1.0f, |
Mike Stroyan | 16b3d98 | 2015-03-19 14:29:04 -0600 | [diff] [blame] | 199 | 1.0f, 1.0f,-1.0f, |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 200 | |
Chia-I Wu | ae3b55d | 2015-04-22 14:56:17 +0800 | [diff] [blame] | 201 | -1.0f,-1.0f,-1.0f, // -Y side |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 202 | 1.0f,-1.0f,-1.0f, |
Mike Stroyan | 16b3d98 | 2015-03-19 14:29:04 -0600 | [diff] [blame] | 203 | 1.0f,-1.0f, 1.0f, |
Chia-I Wu | ae3b55d | 2015-04-22 14:56:17 +0800 | [diff] [blame] | 204 | -1.0f,-1.0f,-1.0f, |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 205 | 1.0f,-1.0f, 1.0f, |
Mike Stroyan | 16b3d98 | 2015-03-19 14:29:04 -0600 | [diff] [blame] | 206 | -1.0f,-1.0f, 1.0f, |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 207 | |
Chia-I Wu | ae3b55d | 2015-04-22 14:56:17 +0800 | [diff] [blame] | 208 | -1.0f, 1.0f,-1.0f, // +Y side |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 209 | -1.0f, 1.0f, 1.0f, |
| 210 | 1.0f, 1.0f, 1.0f, |
Chia-I Wu | ae3b55d | 2015-04-22 14:56:17 +0800 | [diff] [blame] | 211 | -1.0f, 1.0f,-1.0f, |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 212 | 1.0f, 1.0f, 1.0f, |
Mike Stroyan | 16b3d98 | 2015-03-19 14:29:04 -0600 | [diff] [blame] | 213 | 1.0f, 1.0f,-1.0f, |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 214 | |
Chia-I Wu | ae3b55d | 2015-04-22 14:56:17 +0800 | [diff] [blame] | 215 | 1.0f, 1.0f,-1.0f, // +X side |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 216 | 1.0f, 1.0f, 1.0f, |
| 217 | 1.0f,-1.0f, 1.0f, |
Chia-I Wu | ae3b55d | 2015-04-22 14:56:17 +0800 | [diff] [blame] | 218 | 1.0f,-1.0f, 1.0f, |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 219 | 1.0f,-1.0f,-1.0f, |
| 220 | 1.0f, 1.0f,-1.0f, |
| 221 | |
Chia-I Wu | ae3b55d | 2015-04-22 14:56:17 +0800 | [diff] [blame] | 222 | -1.0f, 1.0f, 1.0f, // +Z side |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 223 | -1.0f,-1.0f, 1.0f, |
Mike Stroyan | 16b3d98 | 2015-03-19 14:29:04 -0600 | [diff] [blame] | 224 | 1.0f, 1.0f, 1.0f, |
Chia-I Wu | ae3b55d | 2015-04-22 14:56:17 +0800 | [diff] [blame] | 225 | -1.0f,-1.0f, 1.0f, |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 226 | 1.0f,-1.0f, 1.0f, |
| 227 | 1.0f, 1.0f, 1.0f, |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 228 | }; |
| 229 | |
Mark Lobodzinski | c4fffc7 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 230 | static const float g_uv_buffer_data[] = { |
Rene Lindsay | 76867e8 | 2016-07-29 10:49:11 -0700 | [diff] [blame] | 231 | 0.0f, 1.0f, // -X side |
| 232 | 1.0f, 1.0f, |
Chia-I Wu | ae3b55d | 2015-04-22 14:56:17 +0800 | [diff] [blame] | 233 | 1.0f, 0.0f, |
Chia-I Wu | ae3b55d | 2015-04-22 14:56:17 +0800 | [diff] [blame] | 234 | 1.0f, 0.0f, |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 235 | 0.0f, 0.0f, |
| 236 | 0.0f, 1.0f, |
| 237 | |
Rene Lindsay | 76867e8 | 2016-07-29 10:49:11 -0700 | [diff] [blame] | 238 | 1.0f, 1.0f, // -Z side |
| 239 | 0.0f, 0.0f, |
| 240 | 0.0f, 1.0f, |
| 241 | 1.0f, 1.0f, |
| 242 | 1.0f, 0.0f, |
| 243 | 0.0f, 0.0f, |
| 244 | |
| 245 | 1.0f, 0.0f, // -Y side |
| 246 | 1.0f, 1.0f, |
| 247 | 0.0f, 1.0f, |
| 248 | 1.0f, 0.0f, |
Courtney Goeltzenleuchter | 8879d3a | 2014-10-29 08:29:35 -0600 | [diff] [blame] | 249 | 0.0f, 1.0f, |
Chia-I Wu | ae3b55d | 2015-04-22 14:56:17 +0800 | [diff] [blame] | 250 | 0.0f, 0.0f, |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 251 | |
Rene Lindsay | 76867e8 | 2016-07-29 10:49:11 -0700 | [diff] [blame] | 252 | 1.0f, 0.0f, // +Y side |
| 253 | 0.0f, 0.0f, |
Chia-I Wu | ae3b55d | 2015-04-22 14:56:17 +0800 | [diff] [blame] | 254 | 0.0f, 1.0f, |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 255 | 1.0f, 0.0f, |
Rene Lindsay | 76867e8 | 2016-07-29 10:49:11 -0700 | [diff] [blame] | 256 | 0.0f, 1.0f, |
Mike Stroyan | 16b3d98 | 2015-03-19 14:29:04 -0600 | [diff] [blame] | 257 | 1.0f, 1.0f, |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 258 | |
Rene Lindsay | 76867e8 | 2016-07-29 10:49:11 -0700 | [diff] [blame] | 259 | 1.0f, 0.0f, // +X side |
Chia-I Wu | ae3b55d | 2015-04-22 14:56:17 +0800 | [diff] [blame] | 260 | 0.0f, 0.0f, |
Rene Lindsay | 76867e8 | 2016-07-29 10:49:11 -0700 | [diff] [blame] | 261 | 0.0f, 1.0f, |
| 262 | 0.0f, 1.0f, |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 263 | 1.0f, 1.0f, |
Chia-I Wu | ae3b55d | 2015-04-22 14:56:17 +0800 | [diff] [blame] | 264 | 1.0f, 0.0f, |
Rene Lindsay | 76867e8 | 2016-07-29 10:49:11 -0700 | [diff] [blame] | 265 | |
| 266 | 0.0f, 0.0f, // +Z side |
| 267 | 0.0f, 1.0f, |
| 268 | 1.0f, 0.0f, |
| 269 | 0.0f, 1.0f, |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 270 | 1.0f, 1.0f, |
Rene Lindsay | 76867e8 | 2016-07-29 10:49:11 -0700 | [diff] [blame] | 271 | 1.0f, 0.0f, |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 272 | }; |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 273 | // clang-format on |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 274 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 275 | void dumpMatrix(const char *note, mat4x4 MVP) { |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 276 | int i; |
| 277 | |
| 278 | printf("%s: \n", note); |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 279 | for (i = 0; i < 4; i++) { |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 280 | printf("%f, %f, %f, %f\n", MVP[i][0], MVP[i][1], MVP[i][2], MVP[i][3]); |
| 281 | } |
| 282 | printf("\n"); |
| 283 | fflush(stdout); |
| 284 | } |
| 285 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 286 | void dumpVec4(const char *note, vec4 vector) { |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 287 | printf("%s: \n", note); |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 288 | printf("%f, %f, %f, %f\n", vector[0], vector[1], vector[2], vector[3]); |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 289 | printf("\n"); |
| 290 | fflush(stdout); |
| 291 | } |
| 292 | |
Mark Lobodzinski | 4c62d00 | 2016-05-19 17:22:25 -0600 | [diff] [blame] | 293 | typedef struct { |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 294 | VkImage image; |
Chia-I Wu | a8fe78a | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 295 | VkCommandBuffer cmd; |
Tony Barbour | 22e0627 | 2016-09-07 16:07:56 -0600 | [diff] [blame] | 296 | VkCommandBuffer graphics_to_present_cmd; |
Courtney Goeltzenleuchter | 41866db | 2015-09-01 17:30:39 -0600 | [diff] [blame] | 297 | VkImageView view; |
Tony Barbour | a72d949 | 2017-01-11 13:35:09 -0700 | [diff] [blame] | 298 | VkBuffer uniform_buffer; |
| 299 | VkDeviceMemory uniform_memory; |
Tony-LunarG | 37af49f | 2019-12-19 12:04:19 -0700 | [diff] [blame] | 300 | void *uniform_memory_ptr; |
Tony Barbour | a72d949 | 2017-01-11 13:35:09 -0700 | [diff] [blame] | 301 | VkFramebuffer framebuffer; |
| 302 | VkDescriptorSet descriptor_set; |
Tony Barbour | a72d949 | 2017-01-11 13:35:09 -0700 | [diff] [blame] | 303 | } SwapchainImageResources; |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 304 | |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 305 | struct demo { |
Michael Lentine | c6cde4b | 2016-04-18 13:20:45 -0500 | [diff] [blame] | 306 | #if defined(VK_USE_PLATFORM_WIN32_KHR) |
Ian Elliott | 639ca47 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 307 | #define APP_NAME_STR_LEN 80 |
Mark Lobodzinski | 85dbd82 | 2017-01-26 13:34:13 -0700 | [diff] [blame] | 308 | HINSTANCE connection; // hInstance - Windows Instance |
| 309 | char name[APP_NAME_STR_LEN]; // Name to put on the window/icon |
| 310 | HWND window; // hWnd - window handle |
| 311 | POINT minsize; // minimum window size |
Tony Barbour | 153cb06 | 2016-12-07 13:43:36 -0700 | [diff] [blame] | 312 | #elif defined(VK_USE_PLATFORM_XLIB_KHR) |
Mark Lobodzinski | 2dbc266 | 2017-01-26 12:16:30 -0700 | [diff] [blame] | 313 | Display *display; |
Tony Barbour | 2593b18 | 2016-04-19 10:57:58 -0600 | [diff] [blame] | 314 | Window xlib_window; |
| 315 | Atom xlib_wm_delete_window; |
Tony Barbour | 153cb06 | 2016-12-07 13:43:36 -0700 | [diff] [blame] | 316 | #elif defined(VK_USE_PLATFORM_XCB_KHR) |
Mark Lobodzinski | 2dbc266 | 2017-01-26 12:16:30 -0700 | [diff] [blame] | 317 | Display *display; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 318 | xcb_connection_t *connection; |
| 319 | xcb_screen_t *screen; |
Tony Barbour | 2593b18 | 2016-04-19 10:57:58 -0600 | [diff] [blame] | 320 | xcb_window_t xcb_window; |
Chia-I Wu | cbb564e | 2015-04-16 22:02:10 +0800 | [diff] [blame] | 321 | xcb_intern_atom_reply_t *atom_wm_delete_window; |
Mun Gwan-gyeong | 6f01c55 | 2016-06-27 05:34:44 +0900 | [diff] [blame] | 322 | #elif defined(VK_USE_PLATFORM_WAYLAND_KHR) |
| 323 | struct wl_display *display; |
| 324 | struct wl_registry *registry; |
| 325 | struct wl_compositor *compositor; |
| 326 | struct wl_surface *window; |
Tony-LunarG | 5ceb7be | 2019-12-05 16:05:40 -0700 | [diff] [blame] | 327 | struct wl_shell *shell; |
| 328 | struct wl_shell_surface *shell_surface; |
Joey Bzdek | 15eb070 | 2017-06-07 09:40:36 -0600 | [diff] [blame] | 329 | struct wl_seat *seat; |
| 330 | struct wl_pointer *pointer; |
| 331 | struct wl_keyboard *keyboard; |
Nicolas Caramelli | c8be8c8 | 2020-07-13 17:22:09 +0200 | [diff] [blame] | 332 | #elif defined(VK_USE_PLATFORM_DIRECTFB_EXT) |
| 333 | IDirectFB *dfb; |
| 334 | IDirectFBSurface *window; |
| 335 | IDirectFBEventBuffer *event_buffer; |
Michael Lentine | c6cde4b | 2016-04-18 13:20:45 -0500 | [diff] [blame] | 336 | #elif defined(VK_USE_PLATFORM_ANDROID_KHR) |
Mike Schuchardt | 837d516 | 2018-03-08 12:57:02 -0700 | [diff] [blame] | 337 | struct ANativeWindow *window; |
Bill Hollings | 0a0625a | 2019-07-15 17:39:18 -0400 | [diff] [blame] | 338 | #elif defined(VK_USE_PLATFORM_METAL_EXT) |
| 339 | void *caMetalLayer; |
Michael Lentine | c6cde4b | 2016-04-18 13:20:45 -0500 | [diff] [blame] | 340 | #endif |
Ian Elliott | b08e0d9 | 2015-11-20 11:55:46 -0700 | [diff] [blame] | 341 | VkSurfaceKHR surface; |
Cody Northrop | 1fedb21 | 2015-05-28 11:27:16 -0600 | [diff] [blame] | 342 | bool prepared; |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 343 | bool use_staging_buffer; |
Tony Barbour | 22e0627 | 2016-09-07 16:07:56 -0600 | [diff] [blame] | 344 | bool separate_present_queue; |
Jeremy Kniager | 602e418 | 2018-01-19 10:52:34 -0700 | [diff] [blame] | 345 | bool is_minimized; |
Witold Baryluk | a3b988f | 2021-01-07 19:03:02 +0000 | [diff] [blame] | 346 | int32_t gpu_number; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 347 | |
Ian Elliott | 53622a7 | 2017-03-28 11:06:33 -0600 | [diff] [blame] | 348 | bool VK_KHR_incremental_present_enabled; |
| 349 | |
Ian Elliott | d940ca2 | 2017-03-22 08:31:27 -0600 | [diff] [blame] | 350 | bool VK_GOOGLE_display_timing_enabled; |
| 351 | bool syncd_with_actual_presents; |
| 352 | uint64_t refresh_duration; |
| 353 | uint64_t refresh_duration_multiplier; |
| 354 | uint64_t target_IPD; // image present duration (inverse of frame rate) |
| 355 | uint64_t prev_desired_present_time; |
| 356 | uint32_t next_present_id; |
| 357 | uint32_t last_early_id; // 0 if no early images |
| 358 | uint32_t last_late_id; // 0 if no late images |
| 359 | |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 360 | VkInstance inst; |
Tony Barbour | 72304ef | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 361 | VkPhysicalDevice gpu; |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 362 | VkDevice device; |
Tony Barbour | a2a6781 | 2016-07-18 13:21:06 -0600 | [diff] [blame] | 363 | VkQueue graphics_queue; |
| 364 | VkQueue present_queue; |
| 365 | uint32_t graphics_queue_family_index; |
| 366 | uint32_t present_queue_family_index; |
Tony Barbour | 66a56c3 | 2016-09-21 13:10:56 -0600 | [diff] [blame] | 367 | VkSemaphore image_acquired_semaphores[FRAME_LAG]; |
| 368 | VkSemaphore draw_complete_semaphores[FRAME_LAG]; |
| 369 | VkSemaphore image_ownership_semaphores[FRAME_LAG]; |
Tony Barbour | ecf1b2b | 2015-06-24 16:06:58 -0600 | [diff] [blame] | 370 | VkPhysicalDeviceProperties gpu_props; |
Cody Northrop | 4d3c11d | 2015-08-03 17:04:53 -0600 | [diff] [blame] | 371 | VkQueueFamilyProperties *queue_props; |
Mark Lobodzinski | eadf998 | 2015-07-02 16:49:40 -0600 | [diff] [blame] | 372 | VkPhysicalDeviceMemoryProperties memory_properties; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 373 | |
Tony Barbour | da2bad5 | 2016-01-22 14:36:40 -0700 | [diff] [blame] | 374 | uint32_t enabled_extension_count; |
| 375 | uint32_t enabled_layer_count; |
| 376 | char *extension_names[64]; |
Karl Schultz | 5b423ea | 2016-06-20 19:08:43 -0600 | [diff] [blame] | 377 | char *enabled_layers[64]; |
Tony Barbour | da2bad5 | 2016-01-22 14:36:40 -0700 | [diff] [blame] | 378 | |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 379 | int width, height; |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 380 | VkFormat format; |
Ian Elliott | a078197 | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 381 | VkColorSpaceKHR color_space; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 382 | |
Mark Lobodzinski | 2dbc266 | 2017-01-26 12:16:30 -0700 | [diff] [blame] | 383 | PFN_vkGetPhysicalDeviceSurfaceSupportKHR fpGetPhysicalDeviceSurfaceSupportKHR; |
| 384 | PFN_vkGetPhysicalDeviceSurfaceCapabilitiesKHR fpGetPhysicalDeviceSurfaceCapabilitiesKHR; |
| 385 | PFN_vkGetPhysicalDeviceSurfaceFormatsKHR fpGetPhysicalDeviceSurfaceFormatsKHR; |
| 386 | PFN_vkGetPhysicalDeviceSurfacePresentModesKHR fpGetPhysicalDeviceSurfacePresentModesKHR; |
Ian Elliott | a078197 | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 387 | PFN_vkCreateSwapchainKHR fpCreateSwapchainKHR; |
| 388 | PFN_vkDestroySwapchainKHR fpDestroySwapchainKHR; |
| 389 | PFN_vkGetSwapchainImagesKHR fpGetSwapchainImagesKHR; |
| 390 | PFN_vkAcquireNextImageKHR fpAcquireNextImageKHR; |
| 391 | PFN_vkQueuePresentKHR fpQueuePresentKHR; |
Ian Elliott | d940ca2 | 2017-03-22 08:31:27 -0600 | [diff] [blame] | 392 | PFN_vkGetRefreshCycleDurationGOOGLE fpGetRefreshCycleDurationGOOGLE; |
| 393 | PFN_vkGetPastPresentationTimingGOOGLE fpGetPastPresentationTimingGOOGLE; |
Ian Elliott | a078197 | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 394 | uint32_t swapchainImageCount; |
Ian Elliott | cf074d3 | 2015-10-16 18:02:43 -0600 | [diff] [blame] | 395 | VkSwapchainKHR swapchain; |
Tony Barbour | a72d949 | 2017-01-11 13:35:09 -0700 | [diff] [blame] | 396 | SwapchainImageResources *swapchain_image_resources; |
Tony Barbour | 291d57b | 2016-10-21 14:47:07 -0600 | [diff] [blame] | 397 | VkPresentModeKHR presentMode; |
Tony Barbour | 66a56c3 | 2016-09-21 13:10:56 -0600 | [diff] [blame] | 398 | VkFence fences[FRAME_LAG]; |
Tony Barbour | 66a56c3 | 2016-09-21 13:10:56 -0600 | [diff] [blame] | 399 | int frame_index; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 400 | |
Chia-I Wu | a8fe78a | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 401 | VkCommandPool cmd_pool; |
Tony Barbour | 22e0627 | 2016-09-07 16:07:56 -0600 | [diff] [blame] | 402 | VkCommandPool present_cmd_pool; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 403 | |
| 404 | struct { |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 405 | VkFormat format; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 406 | |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 407 | VkImage image; |
Chia-I Wu | a8fe78a | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 408 | VkMemoryAllocateInfo mem_alloc; |
Mark Lobodzinski | 2dcdfd7 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 409 | VkDeviceMemory mem; |
Courtney Goeltzenleuchter | 41866db | 2015-09-01 17:30:39 -0600 | [diff] [blame] | 410 | VkImageView view; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 411 | } depth; |
| 412 | |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 413 | struct texture_object textures[DEMO_TEXTURE_COUNT]; |
Tony Barbour | 16156cb | 2016-10-19 14:15:49 -0600 | [diff] [blame] | 414 | struct texture_object staging_texture; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 415 | |
Mark Lobodzinski | 85dbd82 | 2017-01-26 13:34:13 -0700 | [diff] [blame] | 416 | VkCommandBuffer cmd; // Buffer for initialization commands |
Mark Lobodzinski | 1cfc772 | 2015-04-17 14:11:39 -0500 | [diff] [blame] | 417 | VkPipelineLayout pipeline_layout; |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 418 | VkDescriptorSetLayout desc_layout; |
Jon Ashburn | c4ab7af | 2015-07-09 15:02:25 -0600 | [diff] [blame] | 419 | VkPipelineCache pipelineCache; |
Chia-I Wu | f973e32 | 2015-07-08 13:34:24 +0800 | [diff] [blame] | 420 | VkRenderPass render_pass; |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 421 | VkPipeline pipeline; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 422 | |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 423 | mat4x4 projection_matrix; |
| 424 | mat4x4 view_matrix; |
| 425 | mat4x4 model_matrix; |
| 426 | |
Mark Lobodzinski | c4fffc7 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 427 | float spin_angle; |
| 428 | float spin_increment; |
Courtney Goeltzenleuchter | 8879d3a | 2014-10-29 08:29:35 -0600 | [diff] [blame] | 429 | bool pause; |
| 430 | |
Tony Barbour | b6dba5c | 2015-07-21 09:04:41 -0600 | [diff] [blame] | 431 | VkShaderModule vert_shader_module; |
| 432 | VkShaderModule frag_shader_module; |
| 433 | |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 434 | VkDescriptorPool desc_pool; |
Chia-I Wu | f973e32 | 2015-07-08 13:34:24 +0800 | [diff] [blame] | 435 | |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 436 | bool quit; |
David Pinedo | 2bb7c93 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 437 | int32_t curFrame; |
| 438 | int32_t frameCount; |
Tony Barbour | 3dddd5d | 2015-04-29 16:19:20 -0600 | [diff] [blame] | 439 | bool validate; |
Tobin Ehlis | 4eb29d6 | 2017-03-14 11:29:01 -0600 | [diff] [blame] | 440 | bool validate_checks_disabled; |
Courtney Goeltzenleuchter | 03f4b77 | 2015-07-22 11:03:51 -0600 | [diff] [blame] | 441 | bool use_break; |
lenny-lunarg | fbe2239 | 2016-06-06 11:07:53 -0600 | [diff] [blame] | 442 | bool suppress_popups; |
Mark Young | 66510e5 | 2017-11-10 10:05:14 -0700 | [diff] [blame] | 443 | |
| 444 | PFN_vkCreateDebugUtilsMessengerEXT CreateDebugUtilsMessengerEXT; |
| 445 | PFN_vkDestroyDebugUtilsMessengerEXT DestroyDebugUtilsMessengerEXT; |
| 446 | PFN_vkSubmitDebugUtilsMessageEXT SubmitDebugUtilsMessageEXT; |
| 447 | PFN_vkCmdBeginDebugUtilsLabelEXT CmdBeginDebugUtilsLabelEXT; |
| 448 | PFN_vkCmdEndDebugUtilsLabelEXT CmdEndDebugUtilsLabelEXT; |
| 449 | PFN_vkCmdInsertDebugUtilsLabelEXT CmdInsertDebugUtilsLabelEXT; |
| 450 | PFN_vkSetDebugUtilsObjectNameEXT SetDebugUtilsObjectNameEXT; |
| 451 | VkDebugUtilsMessengerEXT dbg_messenger; |
Courtney Goeltzenleuchter | 922a0fa | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 452 | |
Mark Lobodzinski | c4fffc7 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 453 | uint32_t current_buffer; |
Tony Barbour | ab2a036 | 2016-09-06 11:40:12 -0600 | [diff] [blame] | 454 | uint32_t queue_family_count; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 455 | }; |
| 456 | |
Mark Young | 66510e5 | 2017-11-10 10:05:14 -0700 | [diff] [blame] | 457 | VKAPI_ATTR VkBool32 VKAPI_CALL debug_messenger_callback(VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity, |
| 458 | VkDebugUtilsMessageTypeFlagsEXT messageType, |
| 459 | const VkDebugUtilsMessengerCallbackDataEXT *pCallbackData, |
| 460 | void *pUserData) { |
| 461 | char prefix[64] = ""; |
| 462 | char *message = (char *)malloc(strlen(pCallbackData->pMessage) + 5000); |
lenny-lunarg | fbe2239 | 2016-06-06 11:07:53 -0600 | [diff] [blame] | 463 | assert(message); |
Mark Young | 66510e5 | 2017-11-10 10:05:14 -0700 | [diff] [blame] | 464 | struct demo *demo = (struct demo *)pUserData; |
lenny-lunarg | fbe2239 | 2016-06-06 11:07:53 -0600 | [diff] [blame] | 465 | |
Mark Young | 66510e5 | 2017-11-10 10:05:14 -0700 | [diff] [blame] | 466 | if (demo->use_break) { |
| 467 | #ifndef WIN32 |
| 468 | raise(SIGTRAP); |
| 469 | #else |
| 470 | DebugBreak(); |
| 471 | #endif |
| 472 | } |
| 473 | |
| 474 | if (messageSeverity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT) { |
| 475 | strcat(prefix, "VERBOSE : "); |
| 476 | } else if (messageSeverity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT) { |
| 477 | strcat(prefix, "INFO : "); |
| 478 | } else if (messageSeverity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT) { |
| 479 | strcat(prefix, "WARNING : "); |
| 480 | } else if (messageSeverity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT) { |
| 481 | strcat(prefix, "ERROR : "); |
| 482 | } |
| 483 | |
| 484 | if (messageType & VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT) { |
| 485 | strcat(prefix, "GENERAL"); |
lenny-lunarg | fbe2239 | 2016-06-06 11:07:53 -0600 | [diff] [blame] | 486 | } else { |
Mark Young | 66510e5 | 2017-11-10 10:05:14 -0700 | [diff] [blame] | 487 | if (messageType & VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT) { |
| 488 | strcat(prefix, "VALIDATION"); |
| 489 | validation_error = 1; |
| 490 | } |
| 491 | if (messageType & VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT) { |
| 492 | if (messageType & VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT) { |
| 493 | strcat(prefix, "|"); |
| 494 | } |
| 495 | strcat(prefix, "PERFORMANCE"); |
| 496 | } |
| 497 | } |
| 498 | |
| 499 | sprintf(message, "%s - Message Id Number: %d | Message Id Name: %s\n\t%s\n", prefix, pCallbackData->messageIdNumber, |
| 500 | pCallbackData->pMessageIdName, pCallbackData->pMessage); |
| 501 | if (pCallbackData->objectCount > 0) { |
| 502 | char tmp_message[500]; |
| 503 | sprintf(tmp_message, "\n\tObjects - %d\n", pCallbackData->objectCount); |
| 504 | strcat(message, tmp_message); |
| 505 | for (uint32_t object = 0; object < pCallbackData->objectCount; ++object) { |
| 506 | if (NULL != pCallbackData->pObjects[object].pObjectName && strlen(pCallbackData->pObjects[object].pObjectName) > 0) { |
| 507 | sprintf(tmp_message, "\t\tObject[%d] - %s, Handle %p, Name \"%s\"\n", object, |
Mark Young | 4421268 | 2018-02-21 15:29:41 -0700 | [diff] [blame] | 508 | string_VkObjectType(pCallbackData->pObjects[object].objectType), |
Mark Young | 66510e5 | 2017-11-10 10:05:14 -0700 | [diff] [blame] | 509 | (void *)(pCallbackData->pObjects[object].objectHandle), pCallbackData->pObjects[object].pObjectName); |
| 510 | } else { |
| 511 | sprintf(tmp_message, "\t\tObject[%d] - %s, Handle %p\n", object, |
Mark Young | 4421268 | 2018-02-21 15:29:41 -0700 | [diff] [blame] | 512 | string_VkObjectType(pCallbackData->pObjects[object].objectType), |
Mark Young | 66510e5 | 2017-11-10 10:05:14 -0700 | [diff] [blame] | 513 | (void *)(pCallbackData->pObjects[object].objectHandle)); |
| 514 | } |
| 515 | strcat(message, tmp_message); |
| 516 | } |
| 517 | } |
| 518 | if (pCallbackData->cmdBufLabelCount > 0) { |
| 519 | char tmp_message[500]; |
| 520 | sprintf(tmp_message, "\n\tCommand Buffer Labels - %d\n", pCallbackData->cmdBufLabelCount); |
| 521 | strcat(message, tmp_message); |
| 522 | for (uint32_t cmd_buf_label = 0; cmd_buf_label < pCallbackData->cmdBufLabelCount; ++cmd_buf_label) { |
| 523 | sprintf(tmp_message, "\t\tLabel[%d] - %s { %f, %f, %f, %f}\n", cmd_buf_label, |
| 524 | pCallbackData->pCmdBufLabels[cmd_buf_label].pLabelName, pCallbackData->pCmdBufLabels[cmd_buf_label].color[0], |
| 525 | pCallbackData->pCmdBufLabels[cmd_buf_label].color[1], pCallbackData->pCmdBufLabels[cmd_buf_label].color[2], |
| 526 | pCallbackData->pCmdBufLabels[cmd_buf_label].color[3]); |
| 527 | strcat(message, tmp_message); |
| 528 | } |
lenny-lunarg | fbe2239 | 2016-06-06 11:07:53 -0600 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | #ifdef _WIN32 |
Cody Northrop | af28a53 | 2016-09-27 10:50:57 -0600 | [diff] [blame] | 532 | |
Tony Barbour | 602ca4f | 2016-09-12 14:02:43 -0600 | [diff] [blame] | 533 | in_callback = true; |
Jeremy Kniager | 979b531 | 2019-11-22 14:55:57 -0700 | [diff] [blame] | 534 | if (!demo->suppress_popups) MessageBox(NULL, message, "Alert", MB_OK); |
Tony Barbour | 602ca4f | 2016-09-12 14:02:43 -0600 | [diff] [blame] | 535 | in_callback = false; |
Cody Northrop | af28a53 | 2016-09-27 10:50:57 -0600 | [diff] [blame] | 536 | |
Cody Northrop | c5e5a8c | 2016-09-26 21:05:00 -0600 | [diff] [blame] | 537 | #elif defined(ANDROID) |
Cody Northrop | af28a53 | 2016-09-27 10:50:57 -0600 | [diff] [blame] | 538 | |
Mike Schuchardt | 837d516 | 2018-03-08 12:57:02 -0700 | [diff] [blame] | 539 | if (messageSeverity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT) { |
Jeremy Kniager | 979b531 | 2019-11-22 14:55:57 -0700 | [diff] [blame] | 540 | __android_log_print(ANDROID_LOG_INFO, APP_SHORT_NAME, "%s", message); |
Mike Schuchardt | 837d516 | 2018-03-08 12:57:02 -0700 | [diff] [blame] | 541 | } else if (messageSeverity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT) { |
Jeremy Kniager | 979b531 | 2019-11-22 14:55:57 -0700 | [diff] [blame] | 542 | __android_log_print(ANDROID_LOG_WARN, APP_SHORT_NAME, "%s", message); |
Mike Schuchardt | 837d516 | 2018-03-08 12:57:02 -0700 | [diff] [blame] | 543 | } else if (messageSeverity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT) { |
Cody Northrop | af28a53 | 2016-09-27 10:50:57 -0600 | [diff] [blame] | 544 | __android_log_print(ANDROID_LOG_ERROR, APP_SHORT_NAME, "%s", message); |
Mike Schuchardt | 837d516 | 2018-03-08 12:57:02 -0700 | [diff] [blame] | 545 | } else if (messageSeverity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT) { |
| 546 | __android_log_print(ANDROID_LOG_VERBOSE, APP_SHORT_NAME, "%s", message); |
Cody Northrop | c5e5a8c | 2016-09-26 21:05:00 -0600 | [diff] [blame] | 547 | } else { |
Jeremy Kniager | 979b531 | 2019-11-22 14:55:57 -0700 | [diff] [blame] | 548 | __android_log_print(ANDROID_LOG_INFO, APP_SHORT_NAME, "%s", message); |
Cody Northrop | c5e5a8c | 2016-09-26 21:05:00 -0600 | [diff] [blame] | 549 | } |
Cody Northrop | af28a53 | 2016-09-27 10:50:57 -0600 | [diff] [blame] | 550 | |
lenny-lunarg | fbe2239 | 2016-06-06 11:07:53 -0600 | [diff] [blame] | 551 | #else |
Cody Northrop | af28a53 | 2016-09-27 10:50:57 -0600 | [diff] [blame] | 552 | |
lenny-lunarg | fbe2239 | 2016-06-06 11:07:53 -0600 | [diff] [blame] | 553 | printf("%s\n", message); |
| 554 | fflush(stdout); |
Cody Northrop | af28a53 | 2016-09-27 10:50:57 -0600 | [diff] [blame] | 555 | |
lenny-lunarg | fbe2239 | 2016-06-06 11:07:53 -0600 | [diff] [blame] | 556 | #endif |
Cody Northrop | af28a53 | 2016-09-27 10:50:57 -0600 | [diff] [blame] | 557 | |
lenny-lunarg | fbe2239 | 2016-06-06 11:07:53 -0600 | [diff] [blame] | 558 | free(message); |
| 559 | |
Mark Young | 66510e5 | 2017-11-10 10:05:14 -0700 | [diff] [blame] | 560 | // Don't bail out, but keep going. |
lenny-lunarg | fbe2239 | 2016-06-06 11:07:53 -0600 | [diff] [blame] | 561 | return false; |
| 562 | } |
| 563 | |
Ian Elliott | d940ca2 | 2017-03-22 08:31:27 -0600 | [diff] [blame] | 564 | bool ActualTimeLate(uint64_t desired, uint64_t actual, uint64_t rdur) { |
| 565 | // The desired time was the earliest time that the present should have |
| 566 | // occured. In almost every case, the actual time should be later than the |
| 567 | // desired time. We should only consider the actual time "late" if it is |
| 568 | // after "desired + rdur". |
| 569 | if (actual <= desired) { |
| 570 | // The actual time was before or equal to the desired time. This will |
| 571 | // probably never happen, but in case it does, return false since the |
| 572 | // present was obviously NOT late. |
| 573 | return false; |
| 574 | } |
Liam Middlebrook | 881fe39 | 2018-05-03 15:52:32 -0700 | [diff] [blame] | 575 | uint64_t deadline = desired + rdur; |
Ian Elliott | d940ca2 | 2017-03-22 08:31:27 -0600 | [diff] [blame] | 576 | if (actual > deadline) { |
| 577 | return true; |
| 578 | } else { |
| 579 | return false; |
| 580 | } |
| 581 | } |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 582 | bool CanPresentEarlier(uint64_t earliest, uint64_t actual, uint64_t margin, uint64_t rdur) { |
Ian Elliott | d940ca2 | 2017-03-22 08:31:27 -0600 | [diff] [blame] | 583 | if (earliest < actual) { |
| 584 | // Consider whether this present could have occured earlier. Make sure |
| 585 | // that earliest time was at least 2msec earlier than actual time, and |
| 586 | // that the margin was at least 2msec: |
| 587 | uint64_t diff = actual - earliest; |
| 588 | if ((diff >= (2 * MILLION)) && (margin >= (2 * MILLION))) { |
| 589 | // This present could have occured earlier because both: 1) the |
| 590 | // earliest time was at least 2 msec before actual time, and 2) the |
| 591 | // margin was at least 2msec. |
| 592 | return true; |
| 593 | } |
| 594 | } |
| 595 | return false; |
| 596 | } |
| 597 | |
Tony-LunarG | 6cebf14 | 2019-09-11 14:32:23 -0600 | [diff] [blame] | 598 | // Forward declarations: |
Ian Elliott | cf074d3 | 2015-10-16 18:02:43 -0600 | [diff] [blame] | 599 | static void demo_resize(struct demo *demo); |
Tony-LunarG | 6cebf14 | 2019-09-11 14:32:23 -0600 | [diff] [blame] | 600 | static void demo_create_surface(struct demo *demo); |
Ian Elliott | cf074d3 | 2015-10-16 18:02:43 -0600 | [diff] [blame] | 601 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 602 | static bool memory_type_from_properties(struct demo *demo, uint32_t typeBits, VkFlags requirements_mask, uint32_t *typeIndex) { |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 603 | // Search memtypes to find first index with those properties |
Alexandre BACQUART | 89eb8df | 2016-04-28 14:25:09 -0600 | [diff] [blame] | 604 | for (uint32_t i = 0; i < VK_MAX_MEMORY_TYPES; i++) { |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 605 | if ((typeBits & 1) == 1) { |
| 606 | // Type is available, does it match user properties? |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 607 | if ((demo->memory_properties.memoryTypes[i].propertyFlags & requirements_mask) == requirements_mask) { |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 608 | *typeIndex = i; |
| 609 | return true; |
| 610 | } |
| 611 | } |
| 612 | typeBits >>= 1; |
| 613 | } |
| 614 | // No memory types matched, return failure |
| 615 | return false; |
Mark Lobodzinski | eadf998 | 2015-07-02 16:49:40 -0600 | [diff] [blame] | 616 | } |
| 617 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 618 | static void demo_flush_init_cmd(struct demo *demo) { |
Tony Barbour | fdc2d35 | 2015-04-22 09:02:32 -0600 | [diff] [blame] | 619 | VkResult U_ASSERT_ONLY err; |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 620 | |
Tony Barbour | ce7524e | 2016-07-28 12:01:45 -0600 | [diff] [blame] | 621 | // This function could get called twice if the texture uses a staging buffer |
| 622 | // In that case the second call should be ignored |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 623 | if (demo->cmd == VK_NULL_HANDLE) return; |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 624 | |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 625 | err = vkEndCommandBuffer(demo->cmd); |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 626 | assert(!err); |
| 627 | |
Tony Barbour | ce7524e | 2016-07-28 12:01:45 -0600 | [diff] [blame] | 628 | VkFence fence; |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 629 | VkFenceCreateInfo fence_ci = {.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO, .pNext = NULL, .flags = 0}; |
Tony Barbour | a72d949 | 2017-01-11 13:35:09 -0700 | [diff] [blame] | 630 | err = vkCreateFence(demo->device, &fence_ci, NULL, &fence); |
| 631 | assert(!err); |
| 632 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 633 | const VkCommandBuffer cmd_bufs[] = {demo->cmd}; |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 634 | VkSubmitInfo submit_info = {.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO, |
| 635 | .pNext = NULL, |
| 636 | .waitSemaphoreCount = 0, |
| 637 | .pWaitSemaphores = NULL, |
| 638 | .pWaitDstStageMask = NULL, |
| 639 | .commandBufferCount = 1, |
| 640 | .pCommandBuffers = cmd_bufs, |
| 641 | .signalSemaphoreCount = 0, |
| 642 | .pSignalSemaphores = NULL}; |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 643 | |
Tony Barbour | ce7524e | 2016-07-28 12:01:45 -0600 | [diff] [blame] | 644 | err = vkQueueSubmit(demo->graphics_queue, 1, &submit_info, fence); |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 645 | assert(!err); |
| 646 | |
Tony Barbour | ce7524e | 2016-07-28 12:01:45 -0600 | [diff] [blame] | 647 | err = vkWaitForFences(demo->device, 1, &fence, VK_TRUE, UINT64_MAX); |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 648 | assert(!err); |
| 649 | |
Courtney Goeltzenleuchter | 014ded8 | 2015-10-23 14:21:05 -0600 | [diff] [blame] | 650 | vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, cmd_bufs); |
Tony Barbour | ce7524e | 2016-07-28 12:01:45 -0600 | [diff] [blame] | 651 | vkDestroyFence(demo->device, fence, NULL); |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 652 | demo->cmd = VK_NULL_HANDLE; |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 653 | } |
| 654 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 655 | static void demo_set_image_layout(struct demo *demo, VkImage image, VkImageAspectFlags aspectMask, VkImageLayout old_image_layout, |
| 656 | VkImageLayout new_image_layout, VkAccessFlagBits srcAccessMask, VkPipelineStageFlags src_stages, |
Tony Barbour | 5ff1318 | 2016-10-19 13:58:29 -0600 | [diff] [blame] | 657 | VkPipelineStageFlags dest_stages) { |
Tony Barbour | 6db4fcb | 2016-10-19 13:41:16 -0600 | [diff] [blame] | 658 | assert(demo->cmd); |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 659 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 660 | VkImageMemoryBarrier image_memory_barrier = {.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, |
| 661 | .pNext = NULL, |
| 662 | .srcAccessMask = srcAccessMask, |
| 663 | .dstAccessMask = 0, |
Tony-LunarG | a141b96 | 2018-05-30 11:33:19 -0600 | [diff] [blame] | 664 | .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, |
| 665 | .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 666 | .oldLayout = old_image_layout, |
| 667 | .newLayout = new_image_layout, |
| 668 | .image = image, |
| 669 | .subresourceRange = {aspectMask, 0, 1, 0, 1}}; |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 670 | |
Tony Barbour | eb5077b | 2016-10-19 15:23:36 -0600 | [diff] [blame] | 671 | switch (new_image_layout) { |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 672 | case VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL: |
| 673 | /* Make sure anything that was copying from this image has completed */ |
| 674 | image_memory_barrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT; |
| 675 | break; |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 676 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 677 | case VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL: |
| 678 | image_memory_barrier.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT; |
| 679 | break; |
Mark Lobodzinski | 9e0be70 | 2015-11-04 13:49:48 -0700 | [diff] [blame] | 680 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 681 | case VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL: |
| 682 | image_memory_barrier.dstAccessMask = VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT; |
| 683 | break; |
Mark Lobodzinski | 9e0be70 | 2015-11-04 13:49:48 -0700 | [diff] [blame] | 684 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 685 | case VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL: |
| 686 | image_memory_barrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_INPUT_ATTACHMENT_READ_BIT; |
| 687 | break; |
Tony Barbour | eb5077b | 2016-10-19 15:23:36 -0600 | [diff] [blame] | 688 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 689 | case VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL: |
| 690 | image_memory_barrier.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT; |
| 691 | break; |
Tony Barbour | eb5077b | 2016-10-19 15:23:36 -0600 | [diff] [blame] | 692 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 693 | case VK_IMAGE_LAYOUT_PRESENT_SRC_KHR: |
| 694 | image_memory_barrier.dstAccessMask = VK_ACCESS_MEMORY_READ_BIT; |
| 695 | break; |
Tony Barbour | eb5077b | 2016-10-19 15:23:36 -0600 | [diff] [blame] | 696 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 697 | default: |
| 698 | image_memory_barrier.dstAccessMask = 0; |
| 699 | break; |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 700 | } |
| 701 | |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 702 | VkImageMemoryBarrier *pmemory_barrier = &image_memory_barrier; |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 703 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 704 | vkCmdPipelineBarrier(demo->cmd, src_stages, dest_stages, 0, 0, NULL, 0, NULL, 1, pmemory_barrier); |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 705 | } |
| 706 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 707 | static void demo_draw_build_cmd(struct demo *demo, VkCommandBuffer cmd_buf) { |
Lenny Komow | e830b59 | 2018-02-23 16:18:36 -0700 | [diff] [blame] | 708 | VkDebugUtilsLabelEXT label; |
| 709 | memset(&label, 0, sizeof(label)); |
Jon Ashburn | 0bec67e | 2016-01-11 13:12:43 -0700 | [diff] [blame] | 710 | const VkCommandBufferBeginInfo cmd_buf_info = { |
| 711 | .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO, |
| 712 | .pNext = NULL, |
Tony Barbour | ce7524e | 2016-07-28 12:01:45 -0600 | [diff] [blame] | 713 | .flags = VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT, |
Rene Lindsay | d787e3a | 2016-07-07 16:00:14 -0700 | [diff] [blame] | 714 | .pInheritanceInfo = NULL, |
Jon Ashburn | 0bec67e | 2016-01-11 13:12:43 -0700 | [diff] [blame] | 715 | }; |
Chia-I Wu | a74c5b2 | 2015-07-07 11:50:03 +0800 | [diff] [blame] | 716 | const VkClearValue clear_values[2] = { |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 717 | [0] = {.color.float32 = {0.2f, 0.2f, 0.2f, 0.2f}}, |
| 718 | [1] = {.depthStencil = {1.0f, 0}}, |
Chia-I Wu | a74c5b2 | 2015-07-07 11:50:03 +0800 | [diff] [blame] | 719 | }; |
| 720 | const VkRenderPassBeginInfo rp_begin = { |
| 721 | .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO, |
| 722 | .pNext = NULL, |
Chia-I Wu | f973e32 | 2015-07-08 13:34:24 +0800 | [diff] [blame] | 723 | .renderPass = demo->render_pass, |
Tony Barbour | a72d949 | 2017-01-11 13:35:09 -0700 | [diff] [blame] | 724 | .framebuffer = demo->swapchain_image_resources[demo->current_buffer].framebuffer, |
Chia-I Wu | a74c5b2 | 2015-07-07 11:50:03 +0800 | [diff] [blame] | 725 | .renderArea.offset.x = 0, |
| 726 | .renderArea.offset.y = 0, |
| 727 | .renderArea.extent.width = demo->width, |
| 728 | .renderArea.extent.height = demo->height, |
Cody Northrop | 372bbae | 2015-08-04 11:51:03 -0600 | [diff] [blame] | 729 | .clearValueCount = 2, |
| 730 | .pClearValues = clear_values, |
Jon Ashburn | 08f6eb9 | 2015-01-02 18:24:05 -0700 | [diff] [blame] | 731 | }; |
Chia-I Wu | f973e32 | 2015-07-08 13:34:24 +0800 | [diff] [blame] | 732 | VkResult U_ASSERT_ONLY err; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 733 | |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 734 | err = vkBeginCommandBuffer(cmd_buf, &cmd_buf_info); |
Mark Young | 66510e5 | 2017-11-10 10:05:14 -0700 | [diff] [blame] | 735 | |
| 736 | if (demo->validate) { |
| 737 | // Set a name for the command buffer |
| 738 | VkDebugUtilsObjectNameInfoEXT cmd_buf_name = { |
| 739 | .sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_OBJECT_NAME_INFO_EXT, |
| 740 | .pNext = NULL, |
| 741 | .objectType = VK_OBJECT_TYPE_COMMAND_BUFFER, |
| 742 | .objectHandle = (uint64_t)cmd_buf, |
| 743 | .pObjectName = "CubeDrawCommandBuf", |
| 744 | }; |
| 745 | demo->SetDebugUtilsObjectNameEXT(demo->device, &cmd_buf_name); |
| 746 | |
| 747 | label.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_LABEL_EXT; |
| 748 | label.pNext = NULL; |
| 749 | label.pLabelName = "DrawBegin"; |
| 750 | label.color[0] = 0.4f; |
| 751 | label.color[1] = 0.3f; |
| 752 | label.color[2] = 0.2f; |
| 753 | label.color[3] = 0.1f; |
| 754 | demo->CmdBeginDebugUtilsLabelEXT(cmd_buf, &label); |
| 755 | } |
| 756 | |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 757 | assert(!err); |
Chia-I Wu | f051d26 | 2015-10-27 19:25:11 +0800 | [diff] [blame] | 758 | vkCmdBeginRenderPass(cmd_buf, &rp_begin, VK_SUBPASS_CONTENTS_INLINE); |
Mark Young | 66510e5 | 2017-11-10 10:05:14 -0700 | [diff] [blame] | 759 | |
| 760 | if (demo->validate) { |
| 761 | label.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_LABEL_EXT; |
| 762 | label.pNext = NULL; |
| 763 | label.pLabelName = "InsideRenderPass"; |
| 764 | label.color[0] = 8.4f; |
| 765 | label.color[1] = 7.3f; |
| 766 | label.color[2] = 6.2f; |
| 767 | label.color[3] = 7.1f; |
| 768 | demo->CmdBeginDebugUtilsLabelEXT(cmd_buf, &label); |
| 769 | } |
| 770 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 771 | vkCmdBindPipeline(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, demo->pipeline); |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 772 | vkCmdBindDescriptorSets(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, demo->pipeline_layout, 0, 1, |
| 773 | &demo->swapchain_image_resources[demo->current_buffer].descriptor_set, 0, NULL); |
Courtney Goeltzenleuchter | 4cbf78b | 2015-09-17 15:06:17 -0600 | [diff] [blame] | 774 | VkViewport viewport; |
| 775 | memset(&viewport, 0, sizeof(viewport)); |
Jeremy Kniager | 979b531 | 2019-11-22 14:55:57 -0700 | [diff] [blame] | 776 | float viewport_dimension; |
| 777 | if (demo->width < demo->height) { |
| 778 | viewport_dimension = (float)demo->width; |
| 779 | viewport.y = (demo->height - demo->width) / 2.0f; |
| 780 | } else { |
| 781 | viewport_dimension = (float)demo->height; |
| 782 | viewport.x = (demo->width - demo->height) / 2.0f; |
| 783 | } |
| 784 | viewport.height = viewport_dimension; |
| 785 | viewport.width = viewport_dimension; |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 786 | viewport.minDepth = (float)0.0f; |
| 787 | viewport.maxDepth = (float)1.0f; |
Jon Ashburn | 19255f8 | 2015-12-30 14:06:55 -0700 | [diff] [blame] | 788 | vkCmdSetViewport(cmd_buf, 0, 1, &viewport); |
Courtney Goeltzenleuchter | 4cbf78b | 2015-09-17 15:06:17 -0600 | [diff] [blame] | 789 | |
| 790 | VkRect2D scissor; |
| 791 | memset(&scissor, 0, sizeof(scissor)); |
| 792 | scissor.extent.width = demo->width; |
| 793 | scissor.extent.height = demo->height; |
| 794 | scissor.offset.x = 0; |
| 795 | scissor.offset.y = 0; |
Jon Ashburn | 19255f8 | 2015-12-30 14:06:55 -0700 | [diff] [blame] | 796 | vkCmdSetScissor(cmd_buf, 0, 1, &scissor); |
Mark Young | 66510e5 | 2017-11-10 10:05:14 -0700 | [diff] [blame] | 797 | |
| 798 | if (demo->validate) { |
| 799 | label.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_LABEL_EXT; |
| 800 | label.pNext = NULL; |
| 801 | label.pLabelName = "ActualDraw"; |
Mike Schuchardt | 4cf3aa4 | 2018-02-23 12:44:05 -0700 | [diff] [blame] | 802 | label.color[0] = -0.4f; |
| 803 | label.color[1] = -0.3f; |
| 804 | label.color[2] = -0.2f; |
| 805 | label.color[3] = -0.1f; |
Mark Young | 66510e5 | 2017-11-10 10:05:14 -0700 | [diff] [blame] | 806 | demo->CmdBeginDebugUtilsLabelEXT(cmd_buf, &label); |
| 807 | } |
| 808 | |
Courtney Goeltzenleuchter | 332a367 | 2015-09-23 12:31:50 -0600 | [diff] [blame] | 809 | vkCmdDraw(cmd_buf, 12 * 3, 1, 0, 0); |
Mark Young | 66510e5 | 2017-11-10 10:05:14 -0700 | [diff] [blame] | 810 | if (demo->validate) { |
| 811 | demo->CmdEndDebugUtilsLabelEXT(cmd_buf); |
| 812 | } |
| 813 | |
Tony Barbour | 9839039 | 2016-07-28 10:50:13 -0600 | [diff] [blame] | 814 | // Note that ending the renderpass changes the image's layout from |
Tony Barbour | 22e0627 | 2016-09-07 16:07:56 -0600 | [diff] [blame] | 815 | // COLOR_ATTACHMENT_OPTIMAL to PRESENT_SRC_KHR |
Chia-I Wu | 57b23b4 | 2015-06-26 15:34:39 +0800 | [diff] [blame] | 816 | vkCmdEndRenderPass(cmd_buf); |
Mark Young | 66510e5 | 2017-11-10 10:05:14 -0700 | [diff] [blame] | 817 | if (demo->validate) { |
| 818 | demo->CmdEndDebugUtilsLabelEXT(cmd_buf); |
| 819 | } |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 820 | |
Tony Barbour | 22e0627 | 2016-09-07 16:07:56 -0600 | [diff] [blame] | 821 | if (demo->separate_present_queue) { |
| 822 | // We have to transfer ownership from the graphics queue family to the |
| 823 | // present queue family to be able to present. Note that we don't have |
| 824 | // to transfer from present queue family back to graphics queue family at |
| 825 | // the start of the next frame because we don't care about the image's |
| 826 | // contents at that point. |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 827 | VkImageMemoryBarrier image_ownership_barrier = {.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, |
| 828 | .pNext = NULL, |
| 829 | .srcAccessMask = 0, |
Tony-LunarG | 4ba65cd | 2018-05-30 14:53:14 -0600 | [diff] [blame] | 830 | .dstAccessMask = 0, |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 831 | .oldLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR, |
| 832 | .newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR, |
| 833 | .srcQueueFamilyIndex = demo->graphics_queue_family_index, |
| 834 | .dstQueueFamilyIndex = demo->present_queue_family_index, |
| 835 | .image = demo->swapchain_image_resources[demo->current_buffer].image, |
| 836 | .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}}; |
Tony Barbour | 22e0627 | 2016-09-07 16:07:56 -0600 | [diff] [blame] | 837 | |
Tony-LunarG | 4ba65cd | 2018-05-30 14:53:14 -0600 | [diff] [blame] | 838 | vkCmdPipelineBarrier(cmd_buf, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, 0, 0, NULL, 0, |
| 839 | NULL, 1, &image_ownership_barrier); |
Tony Barbour | 22e0627 | 2016-09-07 16:07:56 -0600 | [diff] [blame] | 840 | } |
Mark Young | 66510e5 | 2017-11-10 10:05:14 -0700 | [diff] [blame] | 841 | if (demo->validate) { |
| 842 | demo->CmdEndDebugUtilsLabelEXT(cmd_buf); |
| 843 | } |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 844 | err = vkEndCommandBuffer(cmd_buf); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 845 | assert(!err); |
| 846 | } |
| 847 | |
Tony Barbour | 22e0627 | 2016-09-07 16:07:56 -0600 | [diff] [blame] | 848 | void demo_build_image_ownership_cmd(struct demo *demo, int i) { |
| 849 | VkResult U_ASSERT_ONLY err; |
| 850 | |
| 851 | const VkCommandBufferBeginInfo cmd_buf_info = { |
| 852 | .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO, |
| 853 | .pNext = NULL, |
| 854 | .flags = VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT, |
| 855 | .pInheritanceInfo = NULL, |
| 856 | }; |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 857 | err = vkBeginCommandBuffer(demo->swapchain_image_resources[i].graphics_to_present_cmd, &cmd_buf_info); |
Tony Barbour | 22e0627 | 2016-09-07 16:07:56 -0600 | [diff] [blame] | 858 | assert(!err); |
| 859 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 860 | VkImageMemoryBarrier image_ownership_barrier = {.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, |
| 861 | .pNext = NULL, |
| 862 | .srcAccessMask = 0, |
Tony-LunarG | 4ba65cd | 2018-05-30 14:53:14 -0600 | [diff] [blame] | 863 | .dstAccessMask = 0, |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 864 | .oldLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR, |
| 865 | .newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR, |
| 866 | .srcQueueFamilyIndex = demo->graphics_queue_family_index, |
| 867 | .dstQueueFamilyIndex = demo->present_queue_family_index, |
| 868 | .image = demo->swapchain_image_resources[i].image, |
| 869 | .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}}; |
Tony Barbour | 22e0627 | 2016-09-07 16:07:56 -0600 | [diff] [blame] | 870 | |
Tony-LunarG | 4ba65cd | 2018-05-30 14:53:14 -0600 | [diff] [blame] | 871 | vkCmdPipelineBarrier(demo->swapchain_image_resources[i].graphics_to_present_cmd, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, |
| 872 | VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, 0, 0, NULL, 0, NULL, 1, &image_ownership_barrier); |
Tony Barbour | a72d949 | 2017-01-11 13:35:09 -0700 | [diff] [blame] | 873 | err = vkEndCommandBuffer(demo->swapchain_image_resources[i].graphics_to_present_cmd); |
Tony Barbour | 22e0627 | 2016-09-07 16:07:56 -0600 | [diff] [blame] | 874 | assert(!err); |
| 875 | } |
| 876 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 877 | void demo_update_data_buffer(struct demo *demo) { |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 878 | mat4x4 MVP, Model, VP; |
| 879 | int matrixSize = sizeof(MVP); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 880 | |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 881 | mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 882 | |
Karl Schultz | 6ddf1e0 | 2017-01-04 10:31:20 -0700 | [diff] [blame] | 883 | // Rotate around the Y axis |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 884 | mat4x4_dup(Model, demo->model_matrix); |
Arman Uguray | 3ae0889 | 2021-05-25 00:07:24 -0700 | [diff] [blame] | 885 | mat4x4_rotate_Y(demo->model_matrix, Model, (float)degreesToRadians(demo->spin_angle)); |
| 886 | mat4x4_orthonormalize(demo->model_matrix, demo->model_matrix); |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 887 | mat4x4_mul(MVP, VP, demo->model_matrix); |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 888 | |
Tony-LunarG | 37af49f | 2019-12-19 12:04:19 -0700 | [diff] [blame] | 889 | memcpy(demo->swapchain_image_resources[demo->current_buffer].uniform_memory_ptr, (const void *)&MVP[0][0], matrixSize); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 890 | } |
| 891 | |
Ian Elliott | d940ca2 | 2017-03-22 08:31:27 -0600 | [diff] [blame] | 892 | void DemoUpdateTargetIPD(struct demo *demo) { |
| 893 | // Look at what happened to previous presents, and make appropriate |
| 894 | // adjustments in timing: |
| 895 | VkResult U_ASSERT_ONLY err; |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 896 | VkPastPresentationTimingGOOGLE *past = NULL; |
Ian Elliott | d940ca2 | 2017-03-22 08:31:27 -0600 | [diff] [blame] | 897 | uint32_t count = 0; |
Ian Elliott | d940ca2 | 2017-03-22 08:31:27 -0600 | [diff] [blame] | 898 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 899 | err = demo->fpGetPastPresentationTimingGOOGLE(demo->device, demo->swapchain, &count, NULL); |
Ian Elliott | d940ca2 | 2017-03-22 08:31:27 -0600 | [diff] [blame] | 900 | assert(!err); |
Ian Elliott | d940ca2 | 2017-03-22 08:31:27 -0600 | [diff] [blame] | 901 | if (count) { |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 902 | past = (VkPastPresentationTimingGOOGLE *)malloc(sizeof(VkPastPresentationTimingGOOGLE) * count); |
Ian Elliott | d940ca2 | 2017-03-22 08:31:27 -0600 | [diff] [blame] | 903 | assert(past); |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 904 | err = demo->fpGetPastPresentationTimingGOOGLE(demo->device, demo->swapchain, &count, past); |
Ian Elliott | d940ca2 | 2017-03-22 08:31:27 -0600 | [diff] [blame] | 905 | assert(!err); |
| 906 | |
| 907 | bool early = false; |
| 908 | bool late = false; |
| 909 | bool calibrate_next = false; |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 910 | for (uint32_t i = 0; i < count; i++) { |
Ian Elliott | d940ca2 | 2017-03-22 08:31:27 -0600 | [diff] [blame] | 911 | if (!demo->syncd_with_actual_presents) { |
| 912 | // This is the first time that we've received an |
| 913 | // actualPresentTime for this swapchain. In order to not |
| 914 | // perceive these early frames as "late", we need to sync-up |
| 915 | // our future desiredPresentTime's with the |
| 916 | // actualPresentTime(s) that we're receiving now. |
| 917 | calibrate_next = true; |
Ian Elliott | d940ca2 | 2017-03-22 08:31:27 -0600 | [diff] [blame] | 918 | |
Ian Elliott | d940ca2 | 2017-03-22 08:31:27 -0600 | [diff] [blame] | 919 | // So that we don't suspect any pending presents as late, |
| 920 | // record them all as suspected-late presents: |
| 921 | demo->last_late_id = demo->next_present_id - 1; |
| 922 | demo->last_early_id = 0; |
Ian Elliott | d940ca2 | 2017-03-22 08:31:27 -0600 | [diff] [blame] | 923 | demo->syncd_with_actual_presents = true; |
| 924 | break; |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 925 | } else if (CanPresentEarlier(past[i].earliestPresentTime, past[i].actualPresentTime, past[i].presentMargin, |
Ian Elliott | d940ca2 | 2017-03-22 08:31:27 -0600 | [diff] [blame] | 926 | demo->refresh_duration)) { |
| 927 | // This image could have been presented earlier. We don't want |
| 928 | // to decrease the target_IPD until we've seen early presents |
| 929 | // for at least two seconds. |
| 930 | if (demo->last_early_id == past[i].presentID) { |
| 931 | // We've now seen two seconds worth of early presents. |
| 932 | // Flag it as such, and reset the counter: |
| 933 | early = true; |
| 934 | demo->last_early_id = 0; |
| 935 | } else if (demo->last_early_id == 0) { |
| 936 | // This is the first early present we've seen. |
| 937 | // Calculate the presentID for two seconds from now. |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 938 | uint64_t lastEarlyTime = past[i].actualPresentTime + (2 * BILLION); |
| 939 | uint32_t howManyPresents = (uint32_t)((lastEarlyTime - past[i].actualPresentTime) / demo->target_IPD); |
Ian Elliott | d940ca2 | 2017-03-22 08:31:27 -0600 | [diff] [blame] | 940 | demo->last_early_id = past[i].presentID + howManyPresents; |
| 941 | } else { |
| 942 | // We are in the midst of a set of early images, |
| 943 | // and so we won't do anything. |
| 944 | } |
| 945 | late = false; |
| 946 | demo->last_late_id = 0; |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 947 | } else if (ActualTimeLate(past[i].desiredPresentTime, past[i].actualPresentTime, demo->refresh_duration)) { |
Ian Elliott | d940ca2 | 2017-03-22 08:31:27 -0600 | [diff] [blame] | 948 | // This image was presented after its desired time. Since |
| 949 | // there's a delay between calling vkQueuePresentKHR and when |
| 950 | // we get the timing data, several presents may have been late. |
| 951 | // Thus, we need to threat all of the outstanding presents as |
| 952 | // being likely late, so that we only increase the target_IPD |
| 953 | // once for all of those presents. |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 954 | if ((demo->last_late_id == 0) || (demo->last_late_id < past[i].presentID)) { |
Ian Elliott | d940ca2 | 2017-03-22 08:31:27 -0600 | [diff] [blame] | 955 | late = true; |
| 956 | // Record the last suspected-late present: |
| 957 | demo->last_late_id = demo->next_present_id - 1; |
| 958 | } else { |
| 959 | // We are in the midst of a set of likely-late images, |
| 960 | // and so we won't do anything. |
| 961 | } |
| 962 | early = false; |
| 963 | demo->last_early_id = 0; |
| 964 | } else { |
| 965 | // Since this image was not presented early or late, reset |
| 966 | // any sets of early or late presentIDs: |
| 967 | early = false; |
| 968 | late = false; |
| 969 | calibrate_next = true; |
| 970 | demo->last_early_id = 0; |
| 971 | demo->last_late_id = 0; |
| 972 | } |
| 973 | } |
| 974 | |
| 975 | if (early) { |
| 976 | // Since we've seen at least two-seconds worth of presnts that |
| 977 | // could have occured earlier than desired, let's decrease the |
| 978 | // target_IPD (i.e. increase the frame rate): |
| 979 | // |
| 980 | // TODO(ianelliott): Try to calculate a better target_IPD based |
| 981 | // on the most recently-seen present (this is overly-simplistic). |
| 982 | demo->refresh_duration_multiplier--; |
| 983 | if (demo->refresh_duration_multiplier == 0) { |
| 984 | // This should never happen, but in case it does, don't |
| 985 | // try to go faster. |
| 986 | demo->refresh_duration_multiplier = 1; |
| 987 | } |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 988 | demo->target_IPD = demo->refresh_duration * demo->refresh_duration_multiplier; |
Ian Elliott | d940ca2 | 2017-03-22 08:31:27 -0600 | [diff] [blame] | 989 | } |
| 990 | if (late) { |
| 991 | // Since we found a new instance of a late present, we want to |
| 992 | // increase the target_IPD (i.e. decrease the frame rate): |
| 993 | // |
| 994 | // TODO(ianelliott): Try to calculate a better target_IPD based |
| 995 | // on the most recently-seen present (this is overly-simplistic). |
| 996 | demo->refresh_duration_multiplier++; |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 997 | demo->target_IPD = demo->refresh_duration * demo->refresh_duration_multiplier; |
Ian Elliott | d940ca2 | 2017-03-22 08:31:27 -0600 | [diff] [blame] | 998 | } |
| 999 | |
| 1000 | if (calibrate_next) { |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1001 | int64_t multiple = demo->next_present_id - past[count - 1].presentID; |
| 1002 | demo->prev_desired_present_time = (past[count - 1].actualPresentTime + (multiple * demo->target_IPD)); |
Ian Elliott | d940ca2 | 2017-03-22 08:31:27 -0600 | [diff] [blame] | 1003 | } |
Karl Schultz | a33771f | 2018-05-28 16:16:47 -0600 | [diff] [blame] | 1004 | free(past); |
Ian Elliott | d940ca2 | 2017-03-22 08:31:27 -0600 | [diff] [blame] | 1005 | } |
Ian Elliott | d940ca2 | 2017-03-22 08:31:27 -0600 | [diff] [blame] | 1006 | } |
| 1007 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1008 | static void demo_draw(struct demo *demo) { |
Tony Barbour | fdc2d35 | 2015-04-22 09:02:32 -0600 | [diff] [blame] | 1009 | VkResult U_ASSERT_ONLY err; |
Tony Barbour | e35e8aa | 2016-06-20 10:44:08 -0600 | [diff] [blame] | 1010 | |
Damien Leone | c336d82 | 2017-04-14 16:10:14 -0700 | [diff] [blame] | 1011 | // Ensure no more than FRAME_LAG renderings are outstanding |
szdarkhack | d322ff0 | 2016-10-08 09:51:22 +0300 | [diff] [blame] | 1012 | vkWaitForFences(demo->device, 1, &demo->fences[demo->frame_index], VK_TRUE, UINT64_MAX); |
| 1013 | vkResetFences(demo->device, 1, &demo->fences[demo->frame_index]); |
Tony Barbour | 66a56c3 | 2016-09-21 13:10:56 -0600 | [diff] [blame] | 1014 | |
Tony Barbour | 8b7c911 | 2017-06-29 13:34:41 -0600 | [diff] [blame] | 1015 | do { |
Tony Barbour | 6914e6d | 2017-06-02 12:11:29 -0600 | [diff] [blame] | 1016 | // Get the index of the next available swapchain image: |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1017 | err = |
| 1018 | demo->fpAcquireNextImageKHR(demo->device, demo->swapchain, UINT64_MAX, |
| 1019 | demo->image_acquired_semaphores[demo->frame_index], VK_NULL_HANDLE, &demo->current_buffer); |
Tony Barbour | 66a56c3 | 2016-09-21 13:10:56 -0600 | [diff] [blame] | 1020 | |
Tony Barbour | 6914e6d | 2017-06-02 12:11:29 -0600 | [diff] [blame] | 1021 | if (err == VK_ERROR_OUT_OF_DATE_KHR) { |
| 1022 | // demo->swapchain is out of date (e.g. the window was resized) and |
| 1023 | // must be recreated: |
| 1024 | demo_resize(demo); |
| 1025 | } else if (err == VK_SUBOPTIMAL_KHR) { |
| 1026 | // demo->swapchain is not as optimal as it could be, but the platform's |
| 1027 | // presentation engine will still present the image correctly. |
| 1028 | break; |
Tony-LunarG | 6cebf14 | 2019-09-11 14:32:23 -0600 | [diff] [blame] | 1029 | } else if (err == VK_ERROR_SURFACE_LOST_KHR) { |
| 1030 | vkDestroySurfaceKHR(demo->inst, demo->surface, NULL); |
| 1031 | demo_create_surface(demo); |
| 1032 | demo_resize(demo); |
Tony Barbour | 6914e6d | 2017-06-02 12:11:29 -0600 | [diff] [blame] | 1033 | } else { |
| 1034 | assert(!err); |
| 1035 | } |
Tony Barbour | 8b7c911 | 2017-06-29 13:34:41 -0600 | [diff] [blame] | 1036 | } while (err != VK_SUCCESS); |
Tony Barbour | 6914e6d | 2017-06-02 12:11:29 -0600 | [diff] [blame] | 1037 | |
Tony Barbour | 3eafe1f | 2017-06-14 11:59:55 -0600 | [diff] [blame] | 1038 | demo_update_data_buffer(demo); |
| 1039 | |
Ian Elliott | d940ca2 | 2017-03-22 08:31:27 -0600 | [diff] [blame] | 1040 | if (demo->VK_GOOGLE_display_timing_enabled) { |
| 1041 | // Look at what happened to previous presents, and make appropriate |
| 1042 | // adjustments in timing: |
| 1043 | DemoUpdateTargetIPD(demo); |
| 1044 | |
| 1045 | // Note: a real application would position its geometry to that it's in |
| 1046 | // the correct locatoin for when the next image is presented. It might |
| 1047 | // also wait, so that there's less latency between any input and when |
| 1048 | // the next image is rendered/presented. This demo program is so |
| 1049 | // simple that it doesn't do either of those. |
| 1050 | } |
| 1051 | |
Tony Barbour | ce7524e | 2016-07-28 12:01:45 -0600 | [diff] [blame] | 1052 | // Wait for the image acquired semaphore to be signaled to ensure |
Courtney Goeltzenleuchter | 5fa898d | 2015-10-20 18:04:07 -0600 | [diff] [blame] | 1053 | // that the image won't be rendered to until the presentation |
| 1054 | // engine has fully released ownership to the application, and it is |
| 1055 | // okay to render to the image. |
Tony Barbour | 22e0627 | 2016-09-07 16:07:56 -0600 | [diff] [blame] | 1056 | VkPipelineStageFlags pipe_stage_flags; |
| 1057 | VkSubmitInfo submit_info; |
| 1058 | submit_info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO; |
| 1059 | submit_info.pNext = NULL; |
| 1060 | submit_info.pWaitDstStageMask = &pipe_stage_flags; |
| 1061 | pipe_stage_flags = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT; |
| 1062 | submit_info.waitSemaphoreCount = 1; |
Tony Barbour | 66a56c3 | 2016-09-21 13:10:56 -0600 | [diff] [blame] | 1063 | submit_info.pWaitSemaphores = &demo->image_acquired_semaphores[demo->frame_index]; |
Tony Barbour | 22e0627 | 2016-09-07 16:07:56 -0600 | [diff] [blame] | 1064 | submit_info.commandBufferCount = 1; |
Tony Barbour | a72d949 | 2017-01-11 13:35:09 -0700 | [diff] [blame] | 1065 | submit_info.pCommandBuffers = &demo->swapchain_image_resources[demo->current_buffer].cmd; |
Tony Barbour | 22e0627 | 2016-09-07 16:07:56 -0600 | [diff] [blame] | 1066 | submit_info.signalSemaphoreCount = 1; |
Tony Barbour | 66a56c3 | 2016-09-21 13:10:56 -0600 | [diff] [blame] | 1067 | submit_info.pSignalSemaphores = &demo->draw_complete_semaphores[demo->frame_index]; |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1068 | err = vkQueueSubmit(demo->graphics_queue, 1, &submit_info, demo->fences[demo->frame_index]); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1069 | assert(!err); |
| 1070 | |
Tony Barbour | 22e0627 | 2016-09-07 16:07:56 -0600 | [diff] [blame] | 1071 | if (demo->separate_present_queue) { |
| 1072 | // If we are using separate queues, change image ownership to the |
| 1073 | // present queue before presenting, waiting for the draw complete |
| 1074 | // semaphore and signalling the ownership released semaphore when finished |
Tony Barbour | a72d949 | 2017-01-11 13:35:09 -0700 | [diff] [blame] | 1075 | VkFence nullFence = VK_NULL_HANDLE; |
Tony Barbour | 22e0627 | 2016-09-07 16:07:56 -0600 | [diff] [blame] | 1076 | pipe_stage_flags = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT; |
| 1077 | submit_info.waitSemaphoreCount = 1; |
Tony Barbour | 66a56c3 | 2016-09-21 13:10:56 -0600 | [diff] [blame] | 1078 | submit_info.pWaitSemaphores = &demo->draw_complete_semaphores[demo->frame_index]; |
Tony Barbour | 22e0627 | 2016-09-07 16:07:56 -0600 | [diff] [blame] | 1079 | submit_info.commandBufferCount = 1; |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1080 | submit_info.pCommandBuffers = &demo->swapchain_image_resources[demo->current_buffer].graphics_to_present_cmd; |
Tony Barbour | 22e0627 | 2016-09-07 16:07:56 -0600 | [diff] [blame] | 1081 | submit_info.signalSemaphoreCount = 1; |
Tony Barbour | 66a56c3 | 2016-09-21 13:10:56 -0600 | [diff] [blame] | 1082 | submit_info.pSignalSemaphores = &demo->image_ownership_semaphores[demo->frame_index]; |
Tony Barbour | 22e0627 | 2016-09-07 16:07:56 -0600 | [diff] [blame] | 1083 | err = vkQueueSubmit(demo->present_queue, 1, &submit_info, nullFence); |
| 1084 | assert(!err); |
| 1085 | } |
| 1086 | |
| 1087 | // If we are using separate queues we have to wait for image ownership, |
| 1088 | // otherwise wait for draw complete |
Ian Elliott | a078197 | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 1089 | VkPresentInfoKHR present = { |
| 1090 | .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR, |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 1091 | .pNext = NULL, |
Ian Elliott | cf7f748 | 2016-08-19 03:46:58 -0600 | [diff] [blame] | 1092 | .waitSemaphoreCount = 1, |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1093 | .pWaitSemaphores = (demo->separate_present_queue) ? &demo->image_ownership_semaphores[demo->frame_index] |
| 1094 | : &demo->draw_complete_semaphores[demo->frame_index], |
Ian Elliott | a078197 | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 1095 | .swapchainCount = 1, |
Ian Elliott | b08e0d9 | 2015-11-20 11:55:46 -0700 | [diff] [blame] | 1096 | .pSwapchains = &demo->swapchain, |
| 1097 | .pImageIndices = &demo->current_buffer, |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 1098 | }; |
| 1099 | |
Dave Airlie | 48a68f9 | 2019-04-12 17:04:48 +1000 | [diff] [blame] | 1100 | VkRectLayerKHR rect; |
| 1101 | VkPresentRegionKHR region; |
| 1102 | VkPresentRegionsKHR regions; |
Ian Elliott | 53622a7 | 2017-03-28 11:06:33 -0600 | [diff] [blame] | 1103 | if (demo->VK_KHR_incremental_present_enabled) { |
| 1104 | // If using VK_KHR_incremental_present, we provide a hint of the region |
| 1105 | // that contains changed content relative to the previously-presented |
| 1106 | // image. The implementation can use this hint in order to save |
| 1107 | // work/power (by only copying the region in the hint). The |
| 1108 | // implementation is free to ignore the hint though, and so we must |
| 1109 | // ensure that the entire image has the correctly-drawn content. |
| 1110 | uint32_t eighthOfWidth = demo->width / 8; |
| 1111 | uint32_t eighthOfHeight = demo->height / 8; |
Dave Airlie | 48a68f9 | 2019-04-12 17:04:48 +1000 | [diff] [blame] | 1112 | |
| 1113 | rect.offset.x = eighthOfWidth; |
| 1114 | rect.offset.y = eighthOfHeight; |
| 1115 | rect.extent.width = eighthOfWidth * 6; |
| 1116 | rect.extent.height = eighthOfHeight * 6; |
| 1117 | rect.layer = 0; |
| 1118 | |
| 1119 | region.rectangleCount = 1; |
| 1120 | region.pRectangles = ▭ |
| 1121 | |
| 1122 | regions.sType = VK_STRUCTURE_TYPE_PRESENT_REGIONS_KHR; |
| 1123 | regions.pNext = present.pNext; |
| 1124 | regions.swapchainCount = present.swapchainCount; |
| 1125 | regions.pRegions = ®ion; |
Ian Elliott | 53622a7 | 2017-03-28 11:06:33 -0600 | [diff] [blame] | 1126 | present.pNext = ®ions; |
Ian Elliott | 53622a7 | 2017-03-28 11:06:33 -0600 | [diff] [blame] | 1127 | } |
| 1128 | |
Ian Elliott | d940ca2 | 2017-03-22 08:31:27 -0600 | [diff] [blame] | 1129 | if (demo->VK_GOOGLE_display_timing_enabled) { |
| 1130 | VkPresentTimeGOOGLE ptime; |
| 1131 | if (demo->prev_desired_present_time == 0) { |
| 1132 | // This must be the first present for this swapchain. |
| 1133 | // |
| 1134 | // We don't know where we are relative to the presentation engine's |
| 1135 | // display's refresh cycle. We also don't know how long rendering |
| 1136 | // takes. Let's make a grossly-simplified assumption that the |
| 1137 | // desiredPresentTime should be half way between now and |
| 1138 | // now+target_IPD. We will adjust over time. |
| 1139 | uint64_t curtime = getTimeInNanoseconds(); |
| 1140 | if (curtime == 0) { |
| 1141 | // Since we didn't find out the current time, don't give a |
| 1142 | // desiredPresentTime: |
| 1143 | ptime.desiredPresentTime = 0; |
| 1144 | } else { |
Ian Elliott | d940ca2 | 2017-03-22 08:31:27 -0600 | [diff] [blame] | 1145 | ptime.desiredPresentTime = curtime + (demo->target_IPD >> 1); |
| 1146 | } |
| 1147 | } else { |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1148 | ptime.desiredPresentTime = (demo->prev_desired_present_time + demo->target_IPD); |
Ian Elliott | d940ca2 | 2017-03-22 08:31:27 -0600 | [diff] [blame] | 1149 | } |
| 1150 | ptime.presentID = demo->next_present_id++; |
| 1151 | demo->prev_desired_present_time = ptime.desiredPresentTime; |
Ian Elliott | d940ca2 | 2017-03-22 08:31:27 -0600 | [diff] [blame] | 1152 | |
| 1153 | VkPresentTimesInfoGOOGLE present_time = { |
| 1154 | .sType = VK_STRUCTURE_TYPE_PRESENT_TIMES_INFO_GOOGLE, |
| 1155 | .pNext = present.pNext, |
| 1156 | .swapchainCount = present.swapchainCount, |
| 1157 | .pTimes = &ptime, |
| 1158 | }; |
| 1159 | if (demo->VK_GOOGLE_display_timing_enabled) { |
| 1160 | present.pNext = &present_time; |
Ian Elliott | d940ca2 | 2017-03-22 08:31:27 -0600 | [diff] [blame] | 1161 | } |
| 1162 | } |
| 1163 | |
Tony Barbour | a2a6781 | 2016-07-18 13:21:06 -0600 | [diff] [blame] | 1164 | err = demo->fpQueuePresentKHR(demo->present_queue, &present); |
Tony Barbour | 66a56c3 | 2016-09-21 13:10:56 -0600 | [diff] [blame] | 1165 | demo->frame_index += 1; |
| 1166 | demo->frame_index %= FRAME_LAG; |
| 1167 | |
Ian Elliott | cf074d3 | 2015-10-16 18:02:43 -0600 | [diff] [blame] | 1168 | if (err == VK_ERROR_OUT_OF_DATE_KHR) { |
| 1169 | // demo->swapchain is out of date (e.g. the window was resized) and |
| 1170 | // must be recreated: |
| 1171 | demo_resize(demo); |
| 1172 | } else if (err == VK_SUBOPTIMAL_KHR) { |
Tony-LunarG | a8e9634 | 2021-06-04 15:12:55 -0600 | [diff] [blame] | 1173 | // SUBOPTIMAL could be due to a resize |
| 1174 | VkSurfaceCapabilitiesKHR surfCapabilities; |
| 1175 | err = demo->fpGetPhysicalDeviceSurfaceCapabilitiesKHR(demo->gpu, demo->surface, &surfCapabilities); |
| 1176 | assert(!err); |
Tony-LunarG | bbdf584 | 2021-06-11 12:00:30 -0600 | [diff] [blame] | 1177 | if (surfCapabilities.currentExtent.width != (uint32_t)demo->width || |
| 1178 | surfCapabilities.currentExtent.height != (uint32_t)demo->height) { |
Tony-LunarG | a8e9634 | 2021-06-04 15:12:55 -0600 | [diff] [blame] | 1179 | demo_resize(demo); |
| 1180 | } |
Tony-LunarG | 6cebf14 | 2019-09-11 14:32:23 -0600 | [diff] [blame] | 1181 | } else if (err == VK_ERROR_SURFACE_LOST_KHR) { |
| 1182 | vkDestroySurfaceKHR(demo->inst, demo->surface, NULL); |
| 1183 | demo_create_surface(demo); |
| 1184 | demo_resize(demo); |
Ian Elliott | cf074d3 | 2015-10-16 18:02:43 -0600 | [diff] [blame] | 1185 | } else { |
| 1186 | assert(!err); |
| 1187 | } |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1188 | } |
| 1189 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1190 | static void demo_prepare_buffers(struct demo *demo) { |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 1191 | VkResult U_ASSERT_ONLY err; |
Ian Elliott | cf074d3 | 2015-10-16 18:02:43 -0600 | [diff] [blame] | 1192 | VkSwapchainKHR oldSwapchain = demo->swapchain; |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 1193 | |
Ian Elliott | b08e0d9 | 2015-11-20 11:55:46 -0700 | [diff] [blame] | 1194 | // Check the surface capabilities and formats |
| 1195 | VkSurfaceCapabilitiesKHR surfCapabilities; |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1196 | err = demo->fpGetPhysicalDeviceSurfaceCapabilitiesKHR(demo->gpu, demo->surface, &surfCapabilities); |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 1197 | assert(!err); |
| 1198 | |
Ian Elliott | 8528eff | 2015-08-07 15:56:59 -0600 | [diff] [blame] | 1199 | uint32_t presentModeCount; |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1200 | err = demo->fpGetPhysicalDeviceSurfacePresentModesKHR(demo->gpu, demo->surface, &presentModeCount, NULL); |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 1201 | assert(!err); |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1202 | VkPresentModeKHR *presentModes = (VkPresentModeKHR *)malloc(presentModeCount * sizeof(VkPresentModeKHR)); |
Ian Elliott | 8528eff | 2015-08-07 15:56:59 -0600 | [diff] [blame] | 1203 | assert(presentModes); |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1204 | err = demo->fpGetPhysicalDeviceSurfacePresentModesKHR(demo->gpu, demo->surface, &presentModeCount, presentModes); |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 1205 | assert(!err); |
| 1206 | |
Ian Elliott | a078197 | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 1207 | VkExtent2D swapchainExtent; |
Ian Elliott | cf7f748 | 2016-08-19 03:46:58 -0600 | [diff] [blame] | 1208 | // width and height are either both 0xFFFFFFFF, or both not 0xFFFFFFFF. |
| 1209 | if (surfCapabilities.currentExtent.width == 0xFFFFFFFF) { |
| 1210 | // If the surface size is undefined, the size is set to the size |
| 1211 | // of the images requested, which must fit within the minimum and |
| 1212 | // maximum values. |
Ian Elliott | a078197 | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 1213 | swapchainExtent.width = demo->width; |
| 1214 | swapchainExtent.height = demo->height; |
Ian Elliott | cf7f748 | 2016-08-19 03:46:58 -0600 | [diff] [blame] | 1215 | |
| 1216 | if (swapchainExtent.width < surfCapabilities.minImageExtent.width) { |
| 1217 | swapchainExtent.width = surfCapabilities.minImageExtent.width; |
| 1218 | } else if (swapchainExtent.width > surfCapabilities.maxImageExtent.width) { |
| 1219 | swapchainExtent.width = surfCapabilities.maxImageExtent.width; |
| 1220 | } |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1221 | |
Ian Elliott | cf7f748 | 2016-08-19 03:46:58 -0600 | [diff] [blame] | 1222 | if (swapchainExtent.height < surfCapabilities.minImageExtent.height) { |
| 1223 | swapchainExtent.height = surfCapabilities.minImageExtent.height; |
| 1224 | } else if (swapchainExtent.height > surfCapabilities.maxImageExtent.height) { |
| 1225 | swapchainExtent.height = surfCapabilities.maxImageExtent.height; |
| 1226 | } |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1227 | } else { |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 1228 | // If the surface size is defined, the swap chain size must match |
Ian Elliott | b08e0d9 | 2015-11-20 11:55:46 -0700 | [diff] [blame] | 1229 | swapchainExtent = surfCapabilities.currentExtent; |
| 1230 | demo->width = surfCapabilities.currentExtent.width; |
| 1231 | demo->height = surfCapabilities.currentExtent.height; |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 1232 | } |
| 1233 | |
Jeremy Kniager | 602e418 | 2018-01-19 10:52:34 -0700 | [diff] [blame] | 1234 | if (demo->width == 0 || demo->height == 0) { |
| 1235 | demo->is_minimized = true; |
| 1236 | return; |
| 1237 | } else { |
| 1238 | demo->is_minimized = false; |
| 1239 | } |
| 1240 | |
Tony Barbour | 66a56c3 | 2016-09-21 13:10:56 -0600 | [diff] [blame] | 1241 | // The FIFO present mode is guaranteed by the spec to be supported |
Ian Elliott | b18fdde | 2016-10-03 12:11:12 -0600 | [diff] [blame] | 1242 | // and to have no tearing. It's a great default present mode to use. |
Ian Elliott | a078197 | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 1243 | VkPresentModeKHR swapchainPresentMode = VK_PRESENT_MODE_FIFO_KHR; |
Tony Barbour | 291d57b | 2016-10-21 14:47:07 -0600 | [diff] [blame] | 1244 | |
Ian Elliott | b18fdde | 2016-10-03 12:11:12 -0600 | [diff] [blame] | 1245 | // There are times when you may wish to use another present mode. The |
| 1246 | // following code shows how to select them, and the comments provide some |
| 1247 | // reasons you may wish to use them. |
| 1248 | // |
| 1249 | // It should be noted that Vulkan 1.0 doesn't provide a method for |
| 1250 | // synchronizing rendering with the presentation engine's display. There |
| 1251 | // is a method provided for throttling rendering with the display, but |
| 1252 | // there are some presentation engines for which this method will not work. |
| 1253 | // If an application doesn't throttle its rendering, and if it renders much |
| 1254 | // faster than the refresh rate of the display, this can waste power on |
| 1255 | // mobile devices. That is because power is being spent rendering images |
| 1256 | // that may never be seen. |
Tony Barbour | 291d57b | 2016-10-21 14:47:07 -0600 | [diff] [blame] | 1257 | |
Ian Elliott | b18fdde | 2016-10-03 12:11:12 -0600 | [diff] [blame] | 1258 | // VK_PRESENT_MODE_IMMEDIATE_KHR is for applications that don't care about |
| 1259 | // tearing, or have some way of synchronizing their rendering with the |
| 1260 | // display. |
Ian Elliott | b18fdde | 2016-10-03 12:11:12 -0600 | [diff] [blame] | 1261 | // VK_PRESENT_MODE_MAILBOX_KHR may be useful for applications that |
| 1262 | // generally render a new presentable image every refresh cycle, but are |
| 1263 | // occasionally early. In this case, the application wants the new image |
| 1264 | // to be displayed instead of the previously-queued-for-presentation image |
| 1265 | // that has not yet been displayed. |
Ian Elliott | b18fdde | 2016-10-03 12:11:12 -0600 | [diff] [blame] | 1266 | // VK_PRESENT_MODE_FIFO_RELAXED_KHR is for applications that generally |
| 1267 | // render a new presentable image every refresh cycle, but are occasionally |
| 1268 | // late. In this case (perhaps because of stuttering/latency concerns), |
| 1269 | // the application wants the late image to be immediately displayed, even |
| 1270 | // though that may mean some tearing. |
Tony Barbour | 291d57b | 2016-10-21 14:47:07 -0600 | [diff] [blame] | 1271 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1272 | if (demo->presentMode != swapchainPresentMode) { |
Tony Barbour | 291d57b | 2016-10-21 14:47:07 -0600 | [diff] [blame] | 1273 | for (size_t i = 0; i < presentModeCount; ++i) { |
| 1274 | if (presentModes[i] == demo->presentMode) { |
| 1275 | swapchainPresentMode = demo->presentMode; |
| 1276 | break; |
| 1277 | } |
Ian Elliott | b18fdde | 2016-10-03 12:11:12 -0600 | [diff] [blame] | 1278 | } |
| 1279 | } |
Tony Barbour | 291d57b | 2016-10-21 14:47:07 -0600 | [diff] [blame] | 1280 | if (swapchainPresentMode != demo->presentMode) { |
| 1281 | ERR_EXIT("Present mode specified is not supported\n", "Present mode unsupported"); |
| 1282 | } |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 1283 | |
Tony Barbour | 84376fe | 2017-01-06 15:54:22 -0700 | [diff] [blame] | 1284 | // Determine the number of VkImages to use in the swap chain. |
| 1285 | // Application desires to acquire 3 images at a time for triple |
| 1286 | // buffering |
| 1287 | uint32_t desiredNumOfSwapchainImages = 3; |
| 1288 | if (desiredNumOfSwapchainImages < surfCapabilities.minImageCount) { |
| 1289 | desiredNumOfSwapchainImages = surfCapabilities.minImageCount; |
| 1290 | } |
Ian Elliott | cf7f748 | 2016-08-19 03:46:58 -0600 | [diff] [blame] | 1291 | // If maxImageCount is 0, we can ask for as many images as we want; |
| 1292 | // otherwise we're limited to maxImageCount |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1293 | if ((surfCapabilities.maxImageCount > 0) && (desiredNumOfSwapchainImages > surfCapabilities.maxImageCount)) { |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 1294 | // Application must settle for fewer images than desired: |
Ian Elliott | cf7f748 | 2016-08-19 03:46:58 -0600 | [diff] [blame] | 1295 | desiredNumOfSwapchainImages = surfCapabilities.maxImageCount; |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 1296 | } |
| 1297 | |
Tony Barbour | 9fd6d35 | 2015-09-16 15:01:32 -0600 | [diff] [blame] | 1298 | VkSurfaceTransformFlagsKHR preTransform; |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1299 | if (surfCapabilities.supportedTransforms & VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR) { |
Ian Elliott | a7a731c | 2015-12-11 15:52:12 -0700 | [diff] [blame] | 1300 | preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR; |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 1301 | } else { |
Ian Elliott | b08e0d9 | 2015-11-20 11:55:46 -0700 | [diff] [blame] | 1302 | preTransform = surfCapabilities.currentTransform; |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 1303 | } |
| 1304 | |
Tony Barbour | 820b55b | 2017-03-14 08:55:46 -0600 | [diff] [blame] | 1305 | // Find a supported composite alpha mode - one of these is guaranteed to be set |
| 1306 | VkCompositeAlphaFlagBitsKHR compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR; |
| 1307 | VkCompositeAlphaFlagBitsKHR compositeAlphaFlags[4] = { |
| 1308 | VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR, |
| 1309 | VK_COMPOSITE_ALPHA_PRE_MULTIPLIED_BIT_KHR, |
| 1310 | VK_COMPOSITE_ALPHA_POST_MULTIPLIED_BIT_KHR, |
| 1311 | VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR, |
| 1312 | }; |
Tony Barbour | 34ea9cf | 2017-08-14 12:02:30 -0600 | [diff] [blame] | 1313 | for (uint32_t i = 0; i < ARRAY_SIZE(compositeAlphaFlags); i++) { |
Tony Barbour | 820b55b | 2017-03-14 08:55:46 -0600 | [diff] [blame] | 1314 | if (surfCapabilities.supportedCompositeAlpha & compositeAlphaFlags[i]) { |
| 1315 | compositeAlpha = compositeAlphaFlags[i]; |
| 1316 | break; |
| 1317 | } |
| 1318 | } |
| 1319 | |
Tony Barbour | a2a6781 | 2016-07-18 13:21:06 -0600 | [diff] [blame] | 1320 | VkSwapchainCreateInfoKHR swapchain_ci = { |
Ian Elliott | 5b97eae | 2015-09-22 10:20:23 -0600 | [diff] [blame] | 1321 | .sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR, |
Chia-I Wu | cbb564e | 2015-04-16 22:02:10 +0800 | [diff] [blame] | 1322 | .pNext = NULL, |
Ian Elliott | b08e0d9 | 2015-11-20 11:55:46 -0700 | [diff] [blame] | 1323 | .surface = demo->surface, |
Ian Elliott | cf7f748 | 2016-08-19 03:46:58 -0600 | [diff] [blame] | 1324 | .minImageCount = desiredNumOfSwapchainImages, |
Chia-I Wu | cbb564e | 2015-04-16 22:02:10 +0800 | [diff] [blame] | 1325 | .imageFormat = demo->format, |
Ian Elliott | 8528eff | 2015-08-07 15:56:59 -0600 | [diff] [blame] | 1326 | .imageColorSpace = demo->color_space, |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1327 | .imageExtent = |
| 1328 | { |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1329 | .width = swapchainExtent.width, |
| 1330 | .height = swapchainExtent.height, |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1331 | }, |
Ian Elliott | b08e0d9 | 2015-11-20 11:55:46 -0700 | [diff] [blame] | 1332 | .imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT, |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 1333 | .preTransform = preTransform, |
Tony Barbour | 820b55b | 2017-03-14 08:55:46 -0600 | [diff] [blame] | 1334 | .compositeAlpha = compositeAlpha, |
Ian Elliott | b08e0d9 | 2015-11-20 11:55:46 -0700 | [diff] [blame] | 1335 | .imageArrayLayers = 1, |
| 1336 | .imageSharingMode = VK_SHARING_MODE_EXCLUSIVE, |
| 1337 | .queueFamilyIndexCount = 0, |
Ian Elliott | 8528eff | 2015-08-07 15:56:59 -0600 | [diff] [blame] | 1338 | .pQueueFamilyIndices = NULL, |
Ian Elliott | a078197 | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 1339 | .presentMode = swapchainPresentMode, |
Ian Elliott | cf074d3 | 2015-10-16 18:02:43 -0600 | [diff] [blame] | 1340 | .oldSwapchain = oldSwapchain, |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 1341 | .clipped = true, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1342 | }; |
Mark Lobodzinski | c4fffc7 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 1343 | uint32_t i; |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1344 | err = demo->fpCreateSwapchainKHR(demo->device, &swapchain_ci, NULL, &demo->swapchain); |
Chia-I Wu | cbb564e | 2015-04-16 22:02:10 +0800 | [diff] [blame] | 1345 | assert(!err); |
| 1346 | |
Ian Elliott | cf074d3 | 2015-10-16 18:02:43 -0600 | [diff] [blame] | 1347 | // If we just re-created an existing swapchain, we should destroy the old |
| 1348 | // swapchain at this point. |
| 1349 | // Note: destroying the swapchain also cleans up all its associated |
| 1350 | // presentable images once the platform is done with them. |
Chia-I Wu | 1c4086a | 2015-10-26 20:04:44 +0800 | [diff] [blame] | 1351 | if (oldSwapchain != VK_NULL_HANDLE) { |
Ian Elliott | b08e0d9 | 2015-11-20 11:55:46 -0700 | [diff] [blame] | 1352 | demo->fpDestroySwapchainKHR(demo->device, oldSwapchain, NULL); |
Ian Elliott | cf074d3 | 2015-10-16 18:02:43 -0600 | [diff] [blame] | 1353 | } |
| 1354 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1355 | err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain, &demo->swapchainImageCount, NULL); |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 1356 | assert(!err); |
Chia-I Wu | cbb564e | 2015-04-16 22:02:10 +0800 | [diff] [blame] | 1357 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1358 | VkImage *swapchainImages = (VkImage *)malloc(demo->swapchainImageCount * sizeof(VkImage)); |
Ian Elliott | a078197 | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 1359 | assert(swapchainImages); |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1360 | err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain, &demo->swapchainImageCount, swapchainImages); |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 1361 | assert(!err); |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 1362 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1363 | demo->swapchain_image_resources = |
| 1364 | (SwapchainImageResources *)malloc(sizeof(SwapchainImageResources) * demo->swapchainImageCount); |
Tony Barbour | a72d949 | 2017-01-11 13:35:09 -0700 | [diff] [blame] | 1365 | assert(demo->swapchain_image_resources); |
| 1366 | |
Ian Elliott | a078197 | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 1367 | for (i = 0; i < demo->swapchainImageCount; i++) { |
Courtney Goeltzenleuchter | 41866db | 2015-09-01 17:30:39 -0600 | [diff] [blame] | 1368 | VkImageViewCreateInfo color_image_view = { |
| 1369 | .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1370 | .pNext = NULL, |
| 1371 | .format = demo->format, |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1372 | .components = |
| 1373 | { |
Richard S. Wright Jr | ca51bc7 | 2021-01-06 13:03:18 -0500 | [diff] [blame] | 1374 | .r = VK_COMPONENT_SWIZZLE_IDENTITY, |
| 1375 | .g = VK_COMPONENT_SWIZZLE_IDENTITY, |
| 1376 | .b = VK_COMPONENT_SWIZZLE_IDENTITY, |
| 1377 | .a = VK_COMPONENT_SWIZZLE_IDENTITY, |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1378 | }, |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1379 | .subresourceRange = |
| 1380 | {.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT, .baseMipLevel = 0, .levelCount = 1, .baseArrayLayer = 0, .layerCount = 1}, |
Courtney Goeltzenleuchter | 41866db | 2015-09-01 17:30:39 -0600 | [diff] [blame] | 1381 | .viewType = VK_IMAGE_VIEW_TYPE_2D, |
| 1382 | .flags = 0, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1383 | }; |
| 1384 | |
Tony Barbour | a72d949 | 2017-01-11 13:35:09 -0700 | [diff] [blame] | 1385 | demo->swapchain_image_resources[i].image = swapchainImages[i]; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1386 | |
Tony Barbour | a72d949 | 2017-01-11 13:35:09 -0700 | [diff] [blame] | 1387 | color_image_view.image = demo->swapchain_image_resources[i].image; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1388 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1389 | err = vkCreateImageView(demo->device, &color_image_view, NULL, &demo->swapchain_image_resources[i].view); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1390 | assert(!err); |
| 1391 | } |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1392 | |
Ian Elliott | d940ca2 | 2017-03-22 08:31:27 -0600 | [diff] [blame] | 1393 | if (demo->VK_GOOGLE_display_timing_enabled) { |
| 1394 | VkRefreshCycleDurationGOOGLE rc_dur; |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1395 | err = demo->fpGetRefreshCycleDurationGOOGLE(demo->device, demo->swapchain, &rc_dur); |
Ian Elliott | d940ca2 | 2017-03-22 08:31:27 -0600 | [diff] [blame] | 1396 | assert(!err); |
| 1397 | demo->refresh_duration = rc_dur.refreshDuration; |
| 1398 | |
| 1399 | demo->syncd_with_actual_presents = false; |
| 1400 | // Initially target 1X the refresh duration: |
| 1401 | demo->target_IPD = demo->refresh_duration; |
| 1402 | demo->refresh_duration_multiplier = 1; |
| 1403 | demo->prev_desired_present_time = 0; |
| 1404 | demo->next_present_id = 1; |
Ian Elliott | d940ca2 | 2017-03-22 08:31:27 -0600 | [diff] [blame] | 1405 | } |
| 1406 | |
Jason Chen | 5d4fee9 | 2019-04-04 12:45:29 +0800 | [diff] [blame] | 1407 | if (NULL != swapchainImages) { |
| 1408 | free(swapchainImages); |
| 1409 | } |
| 1410 | |
Mark Young | 04fd3d8 | 2016-01-25 14:43:50 -0700 | [diff] [blame] | 1411 | if (NULL != presentModes) { |
| 1412 | free(presentModes); |
| 1413 | } |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1414 | } |
| 1415 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1416 | static void demo_prepare_depth(struct demo *demo) { |
Tony Barbour | 72304ef | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 1417 | const VkFormat depth_format = VK_FORMAT_D16_UNORM; |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1418 | const VkImageCreateInfo image = { |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1419 | .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1420 | .pNext = NULL, |
Tony Barbour | 72304ef | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 1421 | .imageType = VK_IMAGE_TYPE_2D, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1422 | .format = depth_format, |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1423 | .extent = {demo->width, demo->height, 1}, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1424 | .mipLevels = 1, |
Courtney Goeltzenleuchter | dff021f | 2015-10-21 17:57:31 -0600 | [diff] [blame] | 1425 | .arrayLayers = 1, |
Chia-I Wu | 3ede946 | 2015-10-31 00:31:16 +0800 | [diff] [blame] | 1426 | .samples = VK_SAMPLE_COUNT_1_BIT, |
Tony Barbour | 72304ef | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 1427 | .tiling = VK_IMAGE_TILING_OPTIMAL, |
Courtney Goeltzenleuchter | 4e368ee | 2015-09-10 14:14:11 -0600 | [diff] [blame] | 1428 | .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1429 | .flags = 0, |
| 1430 | }; |
Dominik Witczak | 2fe1c70 | 2015-09-22 18:25:33 +0200 | [diff] [blame] | 1431 | |
Courtney Goeltzenleuchter | 41866db | 2015-09-01 17:30:39 -0600 | [diff] [blame] | 1432 | VkImageViewCreateInfo view = { |
| 1433 | .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1434 | .pNext = NULL, |
Chia-I Wu | 1c4086a | 2015-10-26 20:04:44 +0800 | [diff] [blame] | 1435 | .image = VK_NULL_HANDLE, |
Courtney Goeltzenleuchter | 556ee32 | 2015-07-15 17:41:38 -0600 | [diff] [blame] | 1436 | .format = depth_format, |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1437 | .subresourceRange = |
| 1438 | {.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT, .baseMipLevel = 0, .levelCount = 1, .baseArrayLayer = 0, .layerCount = 1}, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1439 | .flags = 0, |
Courtney Goeltzenleuchter | 41866db | 2015-09-01 17:30:39 -0600 | [diff] [blame] | 1440 | .viewType = VK_IMAGE_VIEW_TYPE_2D, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1441 | }; |
Mike Stroyan | ebae832 | 2015-04-17 12:36:38 -0600 | [diff] [blame] | 1442 | |
Mark Lobodzinski | 2dcdfd7 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 1443 | VkMemoryRequirements mem_reqs; |
Tony Barbour | fdc2d35 | 2015-04-22 09:02:32 -0600 | [diff] [blame] | 1444 | VkResult U_ASSERT_ONLY err; |
Courtney Goeltzenleuchter | 87a0ef0 | 2015-10-22 11:03:31 -0600 | [diff] [blame] | 1445 | bool U_ASSERT_ONLY pass; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1446 | |
| 1447 | demo->depth.format = depth_format; |
| 1448 | |
| 1449 | /* create image */ |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1450 | err = vkCreateImage(demo->device, &image, NULL, &demo->depth.image); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1451 | assert(!err); |
| 1452 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1453 | vkGetImageMemoryRequirements(demo->device, demo->depth.image, &mem_reqs); |
Tony Barbour | cb59a5d | 2015-10-23 10:53:30 -0600 | [diff] [blame] | 1454 | assert(!err); |
Mark Lobodzinski | eadf998 | 2015-07-02 16:49:40 -0600 | [diff] [blame] | 1455 | |
Chia-I Wu | f48700b | 2015-11-10 16:21:09 +0800 | [diff] [blame] | 1456 | demo->depth.mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO; |
Dominik Witczak | 2fe1c70 | 2015-09-22 18:25:33 +0200 | [diff] [blame] | 1457 | demo->depth.mem_alloc.pNext = NULL; |
| 1458 | demo->depth.mem_alloc.allocationSize = mem_reqs.size; |
| 1459 | demo->depth.mem_alloc.memoryTypeIndex = 0; |
| 1460 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1461 | pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1462 | &demo->depth.mem_alloc.memoryTypeIndex); |
Courtney Goeltzenleuchter | 87a0ef0 | 2015-10-22 11:03:31 -0600 | [diff] [blame] | 1463 | assert(pass); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1464 | |
Mark Lobodzinski | 2dcdfd7 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 1465 | /* allocate memory */ |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1466 | err = vkAllocateMemory(demo->device, &demo->depth.mem_alloc, NULL, &demo->depth.mem); |
Mark Lobodzinski | 2dcdfd7 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 1467 | assert(!err); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1468 | |
Mark Lobodzinski | 2dcdfd7 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 1469 | /* bind memory */ |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1470 | err = vkBindImageMemory(demo->device, demo->depth.image, demo->depth.mem, 0); |
Mark Lobodzinski | 2dcdfd7 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 1471 | assert(!err); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1472 | |
| 1473 | /* create image view */ |
| 1474 | view.image = demo->depth.image; |
Chia-I Wu | 6363606 | 2015-10-26 21:10:41 +0800 | [diff] [blame] | 1475 | err = vkCreateImageView(demo->device, &view, NULL, &demo->depth.view); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1476 | assert(!err); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1477 | } |
| 1478 | |
Karl Schultz | b794040 | 2018-05-29 13:09:22 -0600 | [diff] [blame] | 1479 | /* Convert ppm image data from header file into RGBA texture image */ |
| 1480 | #include "lunarg.ppm.h" |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1481 | bool loadTexture(const char *filename, uint8_t *rgba_data, VkSubresourceLayout *layout, int32_t *width, int32_t *height) { |
Karl Schultz | b794040 | 2018-05-29 13:09:22 -0600 | [diff] [blame] | 1482 | (void)filename; |
Michael Lentine | c6cde4b | 2016-04-18 13:20:45 -0500 | [diff] [blame] | 1483 | char *cPtr; |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1484 | cPtr = (char *)lunarg_ppm; |
| 1485 | if ((unsigned char *)cPtr >= (lunarg_ppm + lunarg_ppm_len) || strncmp(cPtr, "P6\n", 3)) { |
Michael Lentine | c6cde4b | 2016-04-18 13:20:45 -0500 | [diff] [blame] | 1486 | return false; |
| 1487 | } |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1488 | while (strncmp(cPtr++, "\n", 1)) |
| 1489 | ; |
Alexandre BACQUART | 9063e2a | 2016-06-17 11:30:32 -0600 | [diff] [blame] | 1490 | sscanf(cPtr, "%u %u", width, height); |
Michael Lentine | c6cde4b | 2016-04-18 13:20:45 -0500 | [diff] [blame] | 1491 | if (rgba_data == NULL) { |
| 1492 | return true; |
| 1493 | } |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1494 | while (strncmp(cPtr++, "\n", 1)) |
| 1495 | ; |
| 1496 | if ((unsigned char *)cPtr >= (lunarg_ppm + lunarg_ppm_len) || strncmp(cPtr, "255\n", 4)) { |
Michael Lentine | c6cde4b | 2016-04-18 13:20:45 -0500 | [diff] [blame] | 1497 | return false; |
| 1498 | } |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1499 | while (strncmp(cPtr++, "\n", 1)) |
| 1500 | ; |
Michael Lentine | c6cde4b | 2016-04-18 13:20:45 -0500 | [diff] [blame] | 1501 | for (int y = 0; y < *height; y++) { |
| 1502 | uint8_t *rowPtr = rgba_data; |
| 1503 | for (int x = 0; x < *width; x++) { |
| 1504 | memcpy(rowPtr, cPtr, 3); |
| 1505 | rowPtr[3] = 255; /* Alpha of 1 */ |
| 1506 | rowPtr += 4; |
| 1507 | cPtr += 3; |
| 1508 | } |
| 1509 | rgba_data += layout->rowPitch; |
| 1510 | } |
Michael Lentine | c6cde4b | 2016-04-18 13:20:45 -0500 | [diff] [blame] | 1511 | return true; |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 1512 | } |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 1513 | |
Tony-LunarG | bc9fc05 | 2018-09-21 13:47:06 -0600 | [diff] [blame] | 1514 | static void demo_prepare_texture_buffer(struct demo *demo, const char *filename, struct texture_object *tex_obj) { |
| 1515 | int32_t tex_width; |
| 1516 | int32_t tex_height; |
| 1517 | VkResult U_ASSERT_ONLY err; |
| 1518 | bool U_ASSERT_ONLY pass; |
| 1519 | |
| 1520 | if (!loadTexture(filename, NULL, NULL, &tex_width, &tex_height)) { |
| 1521 | ERR_EXIT("Failed to load textures", "Load Texture Failure"); |
| 1522 | } |
| 1523 | |
| 1524 | tex_obj->tex_width = tex_width; |
| 1525 | tex_obj->tex_height = tex_height; |
| 1526 | |
| 1527 | const VkBufferCreateInfo buffer_create_info = {.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, |
| 1528 | .pNext = NULL, |
| 1529 | .flags = 0, |
| 1530 | .size = tex_width * tex_height * 4, |
| 1531 | .usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT, |
| 1532 | .sharingMode = VK_SHARING_MODE_EXCLUSIVE, |
| 1533 | .queueFamilyIndexCount = 0, |
| 1534 | .pQueueFamilyIndices = NULL}; |
| 1535 | |
| 1536 | err = vkCreateBuffer(demo->device, &buffer_create_info, NULL, &tex_obj->buffer); |
| 1537 | assert(!err); |
| 1538 | |
| 1539 | VkMemoryRequirements mem_reqs; |
| 1540 | vkGetBufferMemoryRequirements(demo->device, tex_obj->buffer, &mem_reqs); |
| 1541 | |
| 1542 | tex_obj->mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO; |
| 1543 | tex_obj->mem_alloc.pNext = NULL; |
| 1544 | tex_obj->mem_alloc.allocationSize = mem_reqs.size; |
| 1545 | tex_obj->mem_alloc.memoryTypeIndex = 0; |
| 1546 | |
| 1547 | VkFlags requirements = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT; |
| 1548 | pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits, requirements, &tex_obj->mem_alloc.memoryTypeIndex); |
| 1549 | assert(pass); |
| 1550 | |
| 1551 | err = vkAllocateMemory(demo->device, &tex_obj->mem_alloc, NULL, &(tex_obj->mem)); |
| 1552 | assert(!err); |
| 1553 | |
| 1554 | /* bind memory */ |
| 1555 | err = vkBindBufferMemory(demo->device, tex_obj->buffer, tex_obj->mem, 0); |
| 1556 | assert(!err); |
| 1557 | |
| 1558 | VkSubresourceLayout layout; |
| 1559 | memset(&layout, 0, sizeof(layout)); |
| 1560 | layout.rowPitch = tex_width * 4; |
| 1561 | |
| 1562 | void *data; |
| 1563 | err = vkMapMemory(demo->device, tex_obj->mem, 0, tex_obj->mem_alloc.allocationSize, 0, &data); |
| 1564 | assert(!err); |
| 1565 | |
| 1566 | if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) { |
| 1567 | fprintf(stderr, "Error loading texture: %s\n", filename); |
| 1568 | } |
| 1569 | |
| 1570 | vkUnmapMemory(demo->device, tex_obj->mem); |
| 1571 | } |
| 1572 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1573 | static void demo_prepare_texture_image(struct demo *demo, const char *filename, struct texture_object *tex_obj, |
| 1574 | VkImageTiling tiling, VkImageUsageFlags usage, VkFlags required_props) { |
Mike Stroyan | 8b89e07 | 2015-06-15 14:21:03 -0600 | [diff] [blame] | 1575 | const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM; |
Mark Lobodzinski | c4fffc7 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 1576 | int32_t tex_width; |
| 1577 | int32_t tex_height; |
Tony Barbour | fdc2d35 | 2015-04-22 09:02:32 -0600 | [diff] [blame] | 1578 | VkResult U_ASSERT_ONLY err; |
Courtney Goeltzenleuchter | 87a0ef0 | 2015-10-22 11:03:31 -0600 | [diff] [blame] | 1579 | bool U_ASSERT_ONLY pass; |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1580 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1581 | if (!loadTexture(filename, NULL, NULL, &tex_width, &tex_height)) { |
Michael Lentine | c6cde4b | 2016-04-18 13:20:45 -0500 | [diff] [blame] | 1582 | ERR_EXIT("Failed to load textures", "Load Texture Failure"); |
David Pinedo | 30bd71d | 2015-04-23 08:16:57 -0600 | [diff] [blame] | 1583 | } |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1584 | |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 1585 | tex_obj->tex_width = tex_width; |
| 1586 | tex_obj->tex_height = tex_height; |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1587 | |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1588 | const VkImageCreateInfo image_create_info = { |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1589 | .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1590 | .pNext = NULL, |
Tony Barbour | 72304ef | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 1591 | .imageType = VK_IMAGE_TYPE_2D, |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1592 | .format = tex_format, |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1593 | .extent = {tex_width, tex_height, 1}, |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1594 | .mipLevels = 1, |
Courtney Goeltzenleuchter | dff021f | 2015-10-21 17:57:31 -0600 | [diff] [blame] | 1595 | .arrayLayers = 1, |
Chia-I Wu | 3ede946 | 2015-10-31 00:31:16 +0800 | [diff] [blame] | 1596 | .samples = VK_SAMPLE_COUNT_1_BIT, |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1597 | .tiling = tiling, |
Courtney Goeltzenleuchter | c56c36a | 2015-04-21 09:31:23 -0600 | [diff] [blame] | 1598 | .usage = usage, |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1599 | .flags = 0, |
Tobin Ehlis | f07d955 | 2016-02-11 17:49:23 -0700 | [diff] [blame] | 1600 | .initialLayout = VK_IMAGE_LAYOUT_PREINITIALIZED, |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1601 | }; |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1602 | |
Mark Lobodzinski | 2dcdfd7 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 1603 | VkMemoryRequirements mem_reqs; |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1604 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1605 | err = vkCreateImage(demo->device, &image_create_info, NULL, &tex_obj->image); |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1606 | assert(!err); |
| 1607 | |
Courtney Goeltzenleuchter | bd5c174 | 2015-10-20 16:40:38 -0600 | [diff] [blame] | 1608 | vkGetImageMemoryRequirements(demo->device, tex_obj->image, &mem_reqs); |
Piers Daniell | 735ee53 | 2015-02-23 16:23:13 -0700 | [diff] [blame] | 1609 | |
Chia-I Wu | f48700b | 2015-11-10 16:21:09 +0800 | [diff] [blame] | 1610 | tex_obj->mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO; |
Dominik Witczak | 2fe1c70 | 2015-09-22 18:25:33 +0200 | [diff] [blame] | 1611 | tex_obj->mem_alloc.pNext = NULL; |
| 1612 | tex_obj->mem_alloc.allocationSize = mem_reqs.size; |
| 1613 | tex_obj->mem_alloc.memoryTypeIndex = 0; |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1614 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1615 | pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits, required_props, &tex_obj->mem_alloc.memoryTypeIndex); |
Courtney Goeltzenleuchter | 87a0ef0 | 2015-10-22 11:03:31 -0600 | [diff] [blame] | 1616 | assert(pass); |
Mark Lobodzinski | eadf998 | 2015-07-02 16:49:40 -0600 | [diff] [blame] | 1617 | |
Mark Lobodzinski | 2dcdfd7 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 1618 | /* allocate memory */ |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1619 | err = vkAllocateMemory(demo->device, &tex_obj->mem_alloc, NULL, &(tex_obj->mem)); |
Mark Lobodzinski | 2dcdfd7 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 1620 | assert(!err); |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1621 | |
Mark Lobodzinski | 2dcdfd7 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 1622 | /* bind memory */ |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1623 | err = vkBindImageMemory(demo->device, tex_obj->image, tex_obj->mem, 0); |
Mark Lobodzinski | 2dcdfd7 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 1624 | assert(!err); |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1625 | |
Tony Barbour | 8a9f62a | 2015-10-15 12:42:56 -0600 | [diff] [blame] | 1626 | if (required_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) { |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1627 | const VkImageSubresource subres = { |
Chia-I Wu | a014143 | 2015-10-27 19:55:05 +0800 | [diff] [blame] | 1628 | .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT, |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1629 | .mipLevel = 0, |
Courtney Goeltzenleuchter | fc465b8 | 2015-09-10 16:38:41 -0600 | [diff] [blame] | 1630 | .arrayLayer = 0, |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1631 | }; |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1632 | VkSubresourceLayout layout; |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1633 | void *data; |
| 1634 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1635 | vkGetImageSubresourceLayout(demo->device, tex_obj->image, &subres, &layout); |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1636 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1637 | err = vkMapMemory(demo->device, tex_obj->mem, 0, tex_obj->mem_alloc.allocationSize, 0, &data); |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1638 | assert(!err); |
| 1639 | |
| 1640 | if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) { |
| 1641 | fprintf(stderr, "Error loading texture: %s\n", filename); |
| 1642 | } |
| 1643 | |
Mark Lobodzinski | e4c92a6 | 2015-09-07 13:59:43 -0600 | [diff] [blame] | 1644 | vkUnmapMemory(demo->device, tex_obj->mem); |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1645 | } |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 1646 | |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1647 | tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL; |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1648 | } |
| 1649 | |
Tony-LunarG | bc9fc05 | 2018-09-21 13:47:06 -0600 | [diff] [blame] | 1650 | static void demo_destroy_texture(struct demo *demo, struct texture_object *tex_objs) { |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1651 | /* clean up staging resources */ |
Chia-I Wu | 6363606 | 2015-10-26 21:10:41 +0800 | [diff] [blame] | 1652 | vkFreeMemory(demo->device, tex_objs->mem, NULL); |
Tony-LunarG | bc9fc05 | 2018-09-21 13:47:06 -0600 | [diff] [blame] | 1653 | if (tex_objs->image) vkDestroyImage(demo->device, tex_objs->image, NULL); |
| 1654 | if (tex_objs->buffer) vkDestroyBuffer(demo->device, tex_objs->buffer, NULL); |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1655 | } |
| 1656 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1657 | static void demo_prepare_textures(struct demo *demo) { |
Tony Barbour | 72304ef | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 1658 | const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM; |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1659 | VkFormatProperties props; |
Mark Lobodzinski | c4fffc7 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 1660 | uint32_t i; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1661 | |
Courtney Goeltzenleuchter | bd5c174 | 2015-10-20 16:40:38 -0600 | [diff] [blame] | 1662 | vkGetPhysicalDeviceFormatProperties(demo->gpu, tex_format, &props); |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1663 | |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1664 | for (i = 0; i < DEMO_TEXTURE_COUNT; i++) { |
Courtney Goeltzenleuchter | bd5c174 | 2015-10-20 16:40:38 -0600 | [diff] [blame] | 1665 | VkResult U_ASSERT_ONLY err; |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1666 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1667 | if ((props.linearTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) && !demo->use_staging_buffer) { |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1668 | /* Device can texture using linear textures */ |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1669 | demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i], VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_SAMPLED_BIT, |
| 1670 | VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT); |
Tony Barbour | 5ff1318 | 2016-10-19 13:58:29 -0600 | [diff] [blame] | 1671 | // Nothing in the pipeline needs to be complete to start, and don't allow fragment |
| 1672 | // shader to run until layout transition completes |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1673 | demo_set_image_layout(demo, demo->textures[i].image, VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_PREINITIALIZED, |
| 1674 | demo->textures[i].imageLayout, 0, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, |
Tony Barbour | 5ff1318 | 2016-10-19 13:58:29 -0600 | [diff] [blame] | 1675 | VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT); |
Tony Barbour | 16156cb | 2016-10-19 14:15:49 -0600 | [diff] [blame] | 1676 | demo->staging_texture.image = 0; |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1677 | } else if (props.optimalTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) { |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1678 | /* Must use staging buffer to copy linear texture to optimized */ |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1679 | |
Tony Barbour | 16156cb | 2016-10-19 14:15:49 -0600 | [diff] [blame] | 1680 | memset(&demo->staging_texture, 0, sizeof(demo->staging_texture)); |
Tony-LunarG | bc9fc05 | 2018-09-21 13:47:06 -0600 | [diff] [blame] | 1681 | demo_prepare_texture_buffer(demo, tex_files[i], &demo->staging_texture); |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1682 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1683 | demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i], VK_IMAGE_TILING_OPTIMAL, |
| 1684 | (VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT), |
| 1685 | VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT); |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1686 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1687 | demo_set_image_layout(demo, demo->textures[i].image, VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_PREINITIALIZED, |
| 1688 | VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 0, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, |
Tony Barbour | 5ff1318 | 2016-10-19 13:58:29 -0600 | [diff] [blame] | 1689 | VK_PIPELINE_STAGE_TRANSFER_BIT); |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1690 | |
Tony-LunarG | bc9fc05 | 2018-09-21 13:47:06 -0600 | [diff] [blame] | 1691 | VkBufferImageCopy copy_region = { |
| 1692 | .bufferOffset = 0, |
| 1693 | .bufferRowLength = demo->staging_texture.tex_width, |
| 1694 | .bufferImageHeight = demo->staging_texture.tex_height, |
| 1695 | .imageSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1}, |
| 1696 | .imageOffset = {0, 0, 0}, |
| 1697 | .imageExtent = {demo->staging_texture.tex_width, demo->staging_texture.tex_height, 1}, |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1698 | }; |
Tony-LunarG | bc9fc05 | 2018-09-21 13:47:06 -0600 | [diff] [blame] | 1699 | |
| 1700 | vkCmdCopyBufferToImage(demo->cmd, demo->staging_texture.buffer, demo->textures[i].image, |
| 1701 | VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, ©_region); |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1702 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1703 | demo_set_image_layout(demo, demo->textures[i].image, VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, |
| 1704 | demo->textures[i].imageLayout, VK_ACCESS_TRANSFER_WRITE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, |
Tony Barbour | 5ff1318 | 2016-10-19 13:58:29 -0600 | [diff] [blame] | 1705 | VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT); |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1706 | |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1707 | } else { |
Mike Stroyan | 8b89e07 | 2015-06-15 14:21:03 -0600 | [diff] [blame] | 1708 | /* Can't support VK_FORMAT_R8G8B8A8_UNORM !? */ |
| 1709 | assert(!"No support for R8G8B8A8_UNORM as texture image format"); |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1710 | } |
| 1711 | |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1712 | const VkSamplerCreateInfo sampler = { |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1713 | .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1714 | .pNext = NULL, |
Chia-I Wu | 6ef8c3b | 2015-10-26 16:49:32 +0800 | [diff] [blame] | 1715 | .magFilter = VK_FILTER_NEAREST, |
| 1716 | .minFilter = VK_FILTER_NEAREST, |
Jon Ashburn | 0bec67e | 2016-01-11 13:12:43 -0700 | [diff] [blame] | 1717 | .mipmapMode = VK_SAMPLER_MIPMAP_MODE_NEAREST, |
Chia-I Wu | 6555f3f | 2015-10-26 17:32:47 +0800 | [diff] [blame] | 1718 | .addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE, |
| 1719 | .addressModeV = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE, |
| 1720 | .addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1721 | .mipLodBias = 0.0f, |
Jon Ashburn | 55e3fb0 | 2015-12-14 14:59:20 -0700 | [diff] [blame] | 1722 | .anisotropyEnable = VK_FALSE, |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1723 | .maxAnisotropy = 1, |
Tony Barbour | 72304ef | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 1724 | .compareOp = VK_COMPARE_OP_NEVER, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1725 | .minLod = 0.0f, |
| 1726 | .maxLod = 0.0f, |
Tony Barbour | cb530c7 | 2015-06-25 16:56:44 -0600 | [diff] [blame] | 1727 | .borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE, |
Mark Lobodzinski | 644dc49 | 2015-09-01 15:42:56 -0600 | [diff] [blame] | 1728 | .unnormalizedCoordinates = VK_FALSE, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1729 | }; |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 1730 | |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1731 | VkImageViewCreateInfo view = { |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1732 | .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1733 | .pNext = NULL, |
Chia-I Wu | 1c4086a | 2015-10-26 20:04:44 +0800 | [diff] [blame] | 1734 | .image = VK_NULL_HANDLE, |
Tony Barbour | 72304ef | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 1735 | .viewType = VK_IMAGE_VIEW_TYPE_2D, |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1736 | .format = tex_format, |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1737 | .components = |
| 1738 | { |
Richard S. Wright Jr | ca51bc7 | 2021-01-06 13:03:18 -0500 | [diff] [blame] | 1739 | VK_COMPONENT_SWIZZLE_IDENTITY, |
| 1740 | VK_COMPONENT_SWIZZLE_IDENTITY, |
| 1741 | VK_COMPONENT_SWIZZLE_IDENTITY, |
| 1742 | VK_COMPONENT_SWIZZLE_IDENTITY, |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1743 | }, |
| 1744 | .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}, |
Courtney Goeltzenleuchter | 41866db | 2015-09-01 17:30:39 -0600 | [diff] [blame] | 1745 | .flags = 0, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1746 | }; |
Jon Ashburn | b2a6665 | 2015-01-16 09:37:43 -0700 | [diff] [blame] | 1747 | |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1748 | /* create sampler */ |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1749 | err = vkCreateSampler(demo->device, &sampler, NULL, &demo->textures[i].sampler); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1750 | assert(!err); |
| 1751 | |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1752 | /* create image view */ |
| 1753 | view.image = demo->textures[i].image; |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1754 | err = vkCreateImageView(demo->device, &view, NULL, &demo->textures[i].view); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1755 | assert(!err); |
| 1756 | } |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1757 | } |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1758 | |
Tony Barbour | a72d949 | 2017-01-11 13:35:09 -0700 | [diff] [blame] | 1759 | void demo_prepare_cube_data_buffers(struct demo *demo) { |
Courtney Goeltzenleuchter | 28c4d5f | 2015-04-14 18:48:46 -0600 | [diff] [blame] | 1760 | VkBufferCreateInfo buf_info; |
Mark Lobodzinski | 2dcdfd7 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 1761 | VkMemoryRequirements mem_reqs; |
Tony Barbour | a72d949 | 2017-01-11 13:35:09 -0700 | [diff] [blame] | 1762 | VkMemoryAllocateInfo mem_alloc; |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 1763 | mat4x4 MVP, VP; |
Tony Barbour | fdc2d35 | 2015-04-22 09:02:32 -0600 | [diff] [blame] | 1764 | VkResult U_ASSERT_ONLY err; |
Courtney Goeltzenleuchter | 87a0ef0 | 2015-10-22 11:03:31 -0600 | [diff] [blame] | 1765 | bool U_ASSERT_ONLY pass; |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1766 | struct vktexcube_vs_uniform data; |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 1767 | |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 1768 | mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix); |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 1769 | mat4x4_mul(MVP, VP, demo->model_matrix); |
| 1770 | memcpy(data.mvp, MVP, sizeof(MVP)); |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1771 | // dumpMatrix("MVP", MVP); |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 1772 | |
Karl Schultz | a261546 | 2017-01-20 13:19:20 -0700 | [diff] [blame] | 1773 | for (unsigned int i = 0; i < 12 * 3; i++) { |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1774 | data.position[i][0] = g_vertex_buffer_data[i * 3]; |
| 1775 | data.position[i][1] = g_vertex_buffer_data[i * 3 + 1]; |
| 1776 | data.position[i][2] = g_vertex_buffer_data[i * 3 + 2]; |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 1777 | data.position[i][3] = 1.0f; |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1778 | data.attr[i][0] = g_uv_buffer_data[2 * i]; |
| 1779 | data.attr[i][1] = g_uv_buffer_data[2 * i + 1]; |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 1780 | data.attr[i][2] = 0; |
| 1781 | data.attr[i][3] = 0; |
| 1782 | } |
| 1783 | |
Chia-I Wu | 8ea21c7 | 2015-01-01 07:55:04 +0800 | [diff] [blame] | 1784 | memset(&buf_info, 0, sizeof(buf_info)); |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1785 | buf_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO; |
Courtney Goeltzenleuchter | 42a078d | 2015-04-15 15:29:59 -0600 | [diff] [blame] | 1786 | buf_info.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT; |
Cody Northrop | 9180730 | 2015-07-16 10:29:32 -0600 | [diff] [blame] | 1787 | buf_info.size = sizeof(data); |
Chia-I Wu | 8ea21c7 | 2015-01-01 07:55:04 +0800 | [diff] [blame] | 1788 | |
Tony Barbour | a72d949 | 2017-01-11 13:35:09 -0700 | [diff] [blame] | 1789 | for (unsigned int i = 0; i < demo->swapchainImageCount; i++) { |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1790 | err = vkCreateBuffer(demo->device, &buf_info, NULL, &demo->swapchain_image_resources[i].uniform_buffer); |
Tony Barbour | a72d949 | 2017-01-11 13:35:09 -0700 | [diff] [blame] | 1791 | assert(!err); |
Chia-I Wu | 8ea21c7 | 2015-01-01 07:55:04 +0800 | [diff] [blame] | 1792 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1793 | vkGetBufferMemoryRequirements(demo->device, demo->swapchain_image_resources[i].uniform_buffer, &mem_reqs); |
Dominik Witczak | 2fe1c70 | 2015-09-22 18:25:33 +0200 | [diff] [blame] | 1794 | |
Tony Barbour | a72d949 | 2017-01-11 13:35:09 -0700 | [diff] [blame] | 1795 | mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO; |
| 1796 | mem_alloc.pNext = NULL; |
| 1797 | mem_alloc.allocationSize = mem_reqs.size; |
| 1798 | mem_alloc.memoryTypeIndex = 0; |
| 1799 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1800 | pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits, |
| 1801 | VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, |
| 1802 | &mem_alloc.memoryTypeIndex); |
Tony Barbour | a72d949 | 2017-01-11 13:35:09 -0700 | [diff] [blame] | 1803 | assert(pass); |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 1804 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1805 | err = vkAllocateMemory(demo->device, &mem_alloc, NULL, &demo->swapchain_image_resources[i].uniform_memory); |
Tony Barbour | a72d949 | 2017-01-11 13:35:09 -0700 | [diff] [blame] | 1806 | assert(!err); |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 1807 | |
Tony-LunarG | 37af49f | 2019-12-19 12:04:19 -0700 | [diff] [blame] | 1808 | err = vkMapMemory(demo->device, demo->swapchain_image_resources[i].uniform_memory, 0, VK_WHOLE_SIZE, 0, |
| 1809 | &demo->swapchain_image_resources[i].uniform_memory_ptr); |
Tony Barbour | a72d949 | 2017-01-11 13:35:09 -0700 | [diff] [blame] | 1810 | assert(!err); |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 1811 | |
Tony-LunarG | 37af49f | 2019-12-19 12:04:19 -0700 | [diff] [blame] | 1812 | memcpy(demo->swapchain_image_resources[i].uniform_memory_ptr, &data, sizeof data); |
Mark Lobodzinski | 2dcdfd7 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 1813 | |
Tony Barbour | a72d949 | 2017-01-11 13:35:09 -0700 | [diff] [blame] | 1814 | err = vkBindBufferMemory(demo->device, demo->swapchain_image_resources[i].uniform_buffer, |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1815 | demo->swapchain_image_resources[i].uniform_memory, 0); |
Tony Barbour | a72d949 | 2017-01-11 13:35:09 -0700 | [diff] [blame] | 1816 | assert(!err); |
| 1817 | } |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 1818 | } |
| 1819 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1820 | static void demo_prepare_descriptor_layout(struct demo *demo) { |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1821 | const VkDescriptorSetLayoutBinding layout_bindings[2] = { |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1822 | [0] = |
| 1823 | { |
| 1824 | .binding = 0, |
| 1825 | .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, |
| 1826 | .descriptorCount = 1, |
| 1827 | .stageFlags = VK_SHADER_STAGE_VERTEX_BIT, |
| 1828 | .pImmutableSamplers = NULL, |
| 1829 | }, |
| 1830 | [1] = |
| 1831 | { |
| 1832 | .binding = 1, |
| 1833 | .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, |
| 1834 | .descriptorCount = DEMO_TEXTURE_COUNT, |
| 1835 | .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT, |
| 1836 | .pImmutableSamplers = NULL, |
| 1837 | }, |
Chia-I Wu | a2aa863 | 2015-03-26 15:04:41 +0800 | [diff] [blame] | 1838 | }; |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1839 | const VkDescriptorSetLayoutCreateInfo descriptor_layout = { |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1840 | .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO, |
Chia-I Wu | 6a3c897 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1841 | .pNext = NULL, |
Chia-I Wu | 3dfc134 | 2015-10-26 20:48:51 +0800 | [diff] [blame] | 1842 | .bindingCount = 2, |
Jon Ashburn | 238f343 | 2015-12-30 18:01:16 -0700 | [diff] [blame] | 1843 | .pBindings = layout_bindings, |
Chia-I Wu | 6a3c897 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1844 | }; |
Tony Barbour | fdc2d35 | 2015-04-22 09:02:32 -0600 | [diff] [blame] | 1845 | VkResult U_ASSERT_ONLY err; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1846 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1847 | err = vkCreateDescriptorSetLayout(demo->device, &descriptor_layout, NULL, &demo->desc_layout); |
Chia-I Wu | b58c24a | 2015-03-26 15:27:55 +0800 | [diff] [blame] | 1848 | assert(!err); |
| 1849 | |
Mark Lobodzinski | 1cfc772 | 2015-04-17 14:11:39 -0500 | [diff] [blame] | 1850 | const VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = { |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1851 | .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO, |
| 1852 | .pNext = NULL, |
Chia-I Wu | 3dfc134 | 2015-10-26 20:48:51 +0800 | [diff] [blame] | 1853 | .setLayoutCount = 1, |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1854 | .pSetLayouts = &demo->desc_layout, |
Mark Lobodzinski | 1cfc772 | 2015-04-17 14:11:39 -0500 | [diff] [blame] | 1855 | }; |
| 1856 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1857 | err = vkCreatePipelineLayout(demo->device, &pPipelineLayoutCreateInfo, NULL, &demo->pipeline_layout); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1858 | assert(!err); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1859 | } |
| 1860 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1861 | static void demo_prepare_render_pass(struct demo *demo) { |
Tony Barbour | db30e4b | 2016-10-11 11:41:05 -0600 | [diff] [blame] | 1862 | // The initial layout for the color and depth attachments will be LAYOUT_UNDEFINED |
| 1863 | // because at the start of the renderpass, we don't care about their contents. |
| 1864 | // At the start of the subpass, the color attachment's layout will be transitioned |
| 1865 | // to LAYOUT_COLOR_ATTACHMENT_OPTIMAL and the depth stencil attachment's layout |
| 1866 | // will be transitioned to LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL. At the end of |
| 1867 | // the renderpass, the color attachment's layout will be transitioned to |
| 1868 | // LAYOUT_PRESENT_SRC_KHR to be ready to present. This is all done as part of |
| 1869 | // the renderpass, no barriers are necessary. |
Chia-I Wu | a74c5b2 | 2015-07-07 11:50:03 +0800 | [diff] [blame] | 1870 | const VkAttachmentDescription attachments[2] = { |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1871 | [0] = |
| 1872 | { |
| 1873 | .format = demo->format, |
| 1874 | .flags = 0, |
| 1875 | .samples = VK_SAMPLE_COUNT_1_BIT, |
| 1876 | .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR, |
| 1877 | .storeOp = VK_ATTACHMENT_STORE_OP_STORE, |
| 1878 | .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE, |
| 1879 | .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE, |
| 1880 | .initialLayout = VK_IMAGE_LAYOUT_UNDEFINED, |
| 1881 | .finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR, |
| 1882 | }, |
| 1883 | [1] = |
| 1884 | { |
| 1885 | .format = demo->depth.format, |
| 1886 | .flags = 0, |
| 1887 | .samples = VK_SAMPLE_COUNT_1_BIT, |
| 1888 | .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR, |
| 1889 | .storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE, |
| 1890 | .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE, |
| 1891 | .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE, |
| 1892 | .initialLayout = VK_IMAGE_LAYOUT_UNDEFINED, |
| 1893 | .finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, |
| 1894 | }, |
Chia-I Wu | f973e32 | 2015-07-08 13:34:24 +0800 | [diff] [blame] | 1895 | }; |
Chia-I Wu | a74c5b2 | 2015-07-07 11:50:03 +0800 | [diff] [blame] | 1896 | const VkAttachmentReference color_reference = { |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1897 | .attachment = 0, |
| 1898 | .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, |
Chia-I Wu | a74c5b2 | 2015-07-07 11:50:03 +0800 | [diff] [blame] | 1899 | }; |
Chia-I Wu | 6555f3f | 2015-10-26 17:32:47 +0800 | [diff] [blame] | 1900 | const VkAttachmentReference depth_reference = { |
| 1901 | .attachment = 1, |
| 1902 | .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, |
| 1903 | }; |
Chia-I Wu | a74c5b2 | 2015-07-07 11:50:03 +0800 | [diff] [blame] | 1904 | const VkSubpassDescription subpass = { |
Chia-I Wu | a74c5b2 | 2015-07-07 11:50:03 +0800 | [diff] [blame] | 1905 | .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS, |
| 1906 | .flags = 0, |
Chia-I Wu | 3dfc134 | 2015-10-26 20:48:51 +0800 | [diff] [blame] | 1907 | .inputAttachmentCount = 0, |
Cody Northrop | 945bb83 | 2015-08-04 11:16:41 -0600 | [diff] [blame] | 1908 | .pInputAttachments = NULL, |
Chia-I Wu | 3dfc134 | 2015-10-26 20:48:51 +0800 | [diff] [blame] | 1909 | .colorAttachmentCount = 1, |
Cody Northrop | 945bb83 | 2015-08-04 11:16:41 -0600 | [diff] [blame] | 1910 | .pColorAttachments = &color_reference, |
| 1911 | .pResolveAttachments = NULL, |
Chia-I Wu | 6555f3f | 2015-10-26 17:32:47 +0800 | [diff] [blame] | 1912 | .pDepthStencilAttachment = &depth_reference, |
Chia-I Wu | 3dfc134 | 2015-10-26 20:48:51 +0800 | [diff] [blame] | 1913 | .preserveAttachmentCount = 0, |
Cody Northrop | 945bb83 | 2015-08-04 11:16:41 -0600 | [diff] [blame] | 1914 | .pPreserveAttachments = NULL, |
Chia-I Wu | a74c5b2 | 2015-07-07 11:50:03 +0800 | [diff] [blame] | 1915 | }; |
Tony-LunarG | 495604b | 2019-06-13 15:32:05 -0600 | [diff] [blame] | 1916 | |
| 1917 | VkSubpassDependency attachmentDependencies[2] = { |
| 1918 | [0] = |
| 1919 | { |
| 1920 | // Depth buffer is shared between swapchain images |
| 1921 | .srcSubpass = VK_SUBPASS_EXTERNAL, |
| 1922 | .dstSubpass = 0, |
| 1923 | .srcStageMask = VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT | VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT, |
| 1924 | .dstStageMask = VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT | VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT, |
| 1925 | .srcAccessMask = VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT, |
| 1926 | .dstAccessMask = VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT, |
| 1927 | .dependencyFlags = 0, |
| 1928 | }, |
| 1929 | [1] = |
| 1930 | { |
| 1931 | // Image Layout Transition |
| 1932 | .srcSubpass = VK_SUBPASS_EXTERNAL, |
| 1933 | .dstSubpass = 0, |
| 1934 | .srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, |
| 1935 | .dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, |
| 1936 | .srcAccessMask = 0, |
| 1937 | .dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT | VK_ACCESS_COLOR_ATTACHMENT_READ_BIT, |
| 1938 | .dependencyFlags = 0, |
| 1939 | }, |
| 1940 | }; |
| 1941 | |
Chia-I Wu | f973e32 | 2015-07-08 13:34:24 +0800 | [diff] [blame] | 1942 | const VkRenderPassCreateInfo rp_info = { |
| 1943 | .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO, |
| 1944 | .pNext = NULL, |
Tony Barbour | a860ce1 | 2016-11-21 12:56:25 -0700 | [diff] [blame] | 1945 | .flags = 0, |
Chia-I Wu | a74c5b2 | 2015-07-07 11:50:03 +0800 | [diff] [blame] | 1946 | .attachmentCount = 2, |
| 1947 | .pAttachments = attachments, |
| 1948 | .subpassCount = 1, |
| 1949 | .pSubpasses = &subpass, |
Tony-LunarG | 495604b | 2019-06-13 15:32:05 -0600 | [diff] [blame] | 1950 | .dependencyCount = 2, |
| 1951 | .pDependencies = attachmentDependencies, |
Chia-I Wu | f973e32 | 2015-07-08 13:34:24 +0800 | [diff] [blame] | 1952 | }; |
Chia-I Wu | a74c5b2 | 2015-07-07 11:50:03 +0800 | [diff] [blame] | 1953 | VkResult U_ASSERT_ONLY err; |
Chia-I Wu | f973e32 | 2015-07-08 13:34:24 +0800 | [diff] [blame] | 1954 | |
Chia-I Wu | 6363606 | 2015-10-26 21:10:41 +0800 | [diff] [blame] | 1955 | err = vkCreateRenderPass(demo->device, &rp_info, NULL, &demo->render_pass); |
Chia-I Wu | f973e32 | 2015-07-08 13:34:24 +0800 | [diff] [blame] | 1956 | assert(!err); |
| 1957 | } |
| 1958 | |
Petr Kraus | 9a4eb6a | 2017-11-30 14:49:20 +0100 | [diff] [blame] | 1959 | static VkShaderModule demo_prepare_shader_module(struct demo *demo, const uint32_t *code, size_t size) { |
Chia-I Wu | 46176f1 | 2015-10-31 00:31:16 +0800 | [diff] [blame] | 1960 | VkShaderModule module; |
Courtney Goeltzenleuchter | a80a1a9 | 2015-06-24 18:24:19 -0600 | [diff] [blame] | 1961 | VkShaderModuleCreateInfo moduleCreateInfo; |
Courtney Goeltzenleuchter | d3cdaaf | 2015-12-17 09:53:29 -0700 | [diff] [blame] | 1962 | VkResult U_ASSERT_ONLY err; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1963 | |
Courtney Goeltzenleuchter | a80a1a9 | 2015-06-24 18:24:19 -0600 | [diff] [blame] | 1964 | moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO; |
| 1965 | moduleCreateInfo.pNext = NULL; |
Petr Kraus | 9a4eb6a | 2017-11-30 14:49:20 +0100 | [diff] [blame] | 1966 | moduleCreateInfo.flags = 0; |
Courtney Goeltzenleuchter | a95e305 | 2015-10-30 15:19:27 -0600 | [diff] [blame] | 1967 | moduleCreateInfo.codeSize = size; |
| 1968 | moduleCreateInfo.pCode = code; |
Petr Kraus | 9a4eb6a | 2017-11-30 14:49:20 +0100 | [diff] [blame] | 1969 | |
Courtney Goeltzenleuchter | a95e305 | 2015-10-30 15:19:27 -0600 | [diff] [blame] | 1970 | err = vkCreateShaderModule(demo->device, &moduleCreateInfo, NULL, &module); |
| 1971 | assert(!err); |
Chia-I Wu | 46176f1 | 2015-10-31 00:31:16 +0800 | [diff] [blame] | 1972 | |
| 1973 | return module; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1974 | } |
| 1975 | |
Petr Kraus | 9a4eb6a | 2017-11-30 14:49:20 +0100 | [diff] [blame] | 1976 | static void demo_prepare_vs(struct demo *demo) { |
| 1977 | const uint32_t vs_code[] = { |
| 1978 | #include "cube.vert.inc" |
| 1979 | }; |
| 1980 | demo->vert_shader_module = demo_prepare_shader_module(demo, vs_code, sizeof(vs_code)); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1981 | } |
| 1982 | |
Petr Kraus | 9a4eb6a | 2017-11-30 14:49:20 +0100 | [diff] [blame] | 1983 | static void demo_prepare_fs(struct demo *demo) { |
| 1984 | const uint32_t fs_code[] = { |
| 1985 | #include "cube.frag.inc" |
| 1986 | }; |
| 1987 | demo->frag_shader_module = demo_prepare_shader_module(demo, fs_code, sizeof(fs_code)); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1988 | } |
| 1989 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1990 | static void demo_prepare_pipeline(struct demo *demo) { |
Tony-LunarG | 8ee48dc | 2020-05-07 10:02:58 -0600 | [diff] [blame] | 1991 | #define NUM_DYNAMIC_STATES 2 /*Viewport + Scissor*/ |
| 1992 | |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1993 | VkGraphicsPipelineCreateInfo pipeline; |
Jon Ashburn | c4ab7af | 2015-07-09 15:02:25 -0600 | [diff] [blame] | 1994 | VkPipelineCacheCreateInfo pipelineCache; |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1995 | VkPipelineVertexInputStateCreateInfo vi; |
Tony Barbour | 194bf28 | 2015-07-10 15:29:03 -0600 | [diff] [blame] | 1996 | VkPipelineInputAssemblyStateCreateInfo ia; |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1997 | VkPipelineRasterizationStateCreateInfo rs; |
| 1998 | VkPipelineColorBlendStateCreateInfo cb; |
| 1999 | VkPipelineDepthStencilStateCreateInfo ds; |
| 2000 | VkPipelineViewportStateCreateInfo vp; |
| 2001 | VkPipelineMultisampleStateCreateInfo ms; |
Tony-LunarG | 8ee48dc | 2020-05-07 10:02:58 -0600 | [diff] [blame] | 2002 | VkDynamicState dynamicStateEnables[NUM_DYNAMIC_STATES]; |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2003 | VkPipelineDynamicStateCreateInfo dynamicState; |
Tony Barbour | fdc2d35 | 2015-04-22 09:02:32 -0600 | [diff] [blame] | 2004 | VkResult U_ASSERT_ONLY err; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 2005 | |
Piers Daniell | ba839d1 | 2015-09-29 13:01:09 -0600 | [diff] [blame] | 2006 | memset(dynamicStateEnables, 0, sizeof dynamicStateEnables); |
| 2007 | memset(&dynamicState, 0, sizeof dynamicState); |
| 2008 | dynamicState.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO; |
| 2009 | dynamicState.pDynamicStates = dynamicStateEnables; |
| 2010 | |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 2011 | memset(&pipeline, 0, sizeof(pipeline)); |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 2012 | pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO; |
Mark Lobodzinski | 1cfc772 | 2015-04-17 14:11:39 -0500 | [diff] [blame] | 2013 | pipeline.layout = demo->pipeline_layout; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 2014 | |
Jason Ekstrand | 515b7ae | 2015-10-15 17:15:25 -0700 | [diff] [blame] | 2015 | memset(&vi, 0, sizeof(vi)); |
| 2016 | vi.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO; |
| 2017 | |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 2018 | memset(&ia, 0, sizeof(ia)); |
Tony Barbour | 194bf28 | 2015-07-10 15:29:03 -0600 | [diff] [blame] | 2019 | ia.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO; |
Tony Barbour | 72304ef | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 2020 | ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 2021 | |
| 2022 | memset(&rs, 0, sizeof(rs)); |
Chia-I Wu | f051d26 | 2015-10-27 19:25:11 +0800 | [diff] [blame] | 2023 | rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO; |
| 2024 | rs.polygonMode = VK_POLYGON_MODE_FILL; |
Chia-I Wu | 6555f3f | 2015-10-26 17:32:47 +0800 | [diff] [blame] | 2025 | rs.cullMode = VK_CULL_MODE_BACK_BIT; |
Chia-I Wu | a8fe78a | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 2026 | rs.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE; |
Courtney Goeltzenleuchter | 0db2c91 | 2015-10-15 12:57:38 -0600 | [diff] [blame] | 2027 | rs.depthClampEnable = VK_FALSE; |
Cody Northrop | 709c174 | 2015-08-17 11:10:49 -0600 | [diff] [blame] | 2028 | rs.rasterizerDiscardEnable = VK_FALSE; |
| 2029 | rs.depthBiasEnable = VK_FALSE; |
Mark Young | 8749e2e | 2016-03-31 14:56:43 -0600 | [diff] [blame] | 2030 | rs.lineWidth = 1.0f; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 2031 | |
| 2032 | memset(&cb, 0, sizeof(cb)); |
Tony Barbour | 194bf28 | 2015-07-10 15:29:03 -0600 | [diff] [blame] | 2033 | cb.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO; |
| 2034 | VkPipelineColorBlendAttachmentState att_state[1]; |
Tony Barbour | 29645d0 | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 2035 | memset(att_state, 0, sizeof(att_state)); |
Chia-I Wu | f051d26 | 2015-10-27 19:25:11 +0800 | [diff] [blame] | 2036 | att_state[0].colorWriteMask = 0xf; |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 2037 | att_state[0].blendEnable = VK_FALSE; |
Tony Barbour | 29645d0 | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 2038 | cb.attachmentCount = 1; |
| 2039 | cb.pAttachments = att_state; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 2040 | |
Tony Barbour | 29645d0 | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 2041 | memset(&vp, 0, sizeof(vp)); |
Tony Barbour | 194bf28 | 2015-07-10 15:29:03 -0600 | [diff] [blame] | 2042 | vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO; |
Tony Barbour | 72304ef | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 2043 | vp.viewportCount = 1; |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 2044 | dynamicStateEnables[dynamicState.dynamicStateCount++] = VK_DYNAMIC_STATE_VIEWPORT; |
Piers Daniell | ba839d1 | 2015-09-29 13:01:09 -0600 | [diff] [blame] | 2045 | vp.scissorCount = 1; |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 2046 | dynamicStateEnables[dynamicState.dynamicStateCount++] = VK_DYNAMIC_STATE_SCISSOR; |
Tony Barbour | 29645d0 | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 2047 | |
| 2048 | memset(&ds, 0, sizeof(ds)); |
Tony Barbour | 194bf28 | 2015-07-10 15:29:03 -0600 | [diff] [blame] | 2049 | ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO; |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 2050 | ds.depthTestEnable = VK_TRUE; |
| 2051 | ds.depthWriteEnable = VK_TRUE; |
Chia-I Wu | a8fe78a | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 2052 | ds.depthCompareOp = VK_COMPARE_OP_LESS_OR_EQUAL; |
Cody Northrop | 5e4125d | 2015-08-26 10:01:32 -0600 | [diff] [blame] | 2053 | ds.depthBoundsTestEnable = VK_FALSE; |
Chia-I Wu | f051d26 | 2015-10-27 19:25:11 +0800 | [diff] [blame] | 2054 | ds.back.failOp = VK_STENCIL_OP_KEEP; |
| 2055 | ds.back.passOp = VK_STENCIL_OP_KEEP; |
| 2056 | ds.back.compareOp = VK_COMPARE_OP_ALWAYS; |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 2057 | ds.stencilTestEnable = VK_FALSE; |
Tony Barbour | 29645d0 | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 2058 | ds.front = ds.back; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 2059 | |
Tony Barbour | 29645d0 | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 2060 | memset(&ms, 0, sizeof(ms)); |
Tony Barbour | 194bf28 | 2015-07-10 15:29:03 -0600 | [diff] [blame] | 2061 | ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO; |
Cody Northrop | 011546b | 2015-08-04 14:34:54 -0600 | [diff] [blame] | 2062 | ms.pSampleMask = NULL; |
Chia-I Wu | 3ede946 | 2015-10-31 00:31:16 +0800 | [diff] [blame] | 2063 | ms.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 2064 | |
Petr Kraus | 9a4eb6a | 2017-11-30 14:49:20 +0100 | [diff] [blame] | 2065 | demo_prepare_vs(demo); |
| 2066 | demo_prepare_fs(demo); |
| 2067 | |
Mark Lobodzinski | 521d776 | 2015-06-23 15:11:57 -0600 | [diff] [blame] | 2068 | // Two stages: vs and fs |
Mark Lobodzinski | 521d776 | 2015-06-23 15:11:57 -0600 | [diff] [blame] | 2069 | VkPipelineShaderStageCreateInfo shaderStages[2]; |
| 2070 | memset(&shaderStages, 0, 2 * sizeof(VkPipelineShaderStageCreateInfo)); |
| 2071 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2072 | shaderStages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO; |
| 2073 | shaderStages[0].stage = VK_SHADER_STAGE_VERTEX_BIT; |
Petr Kraus | 9a4eb6a | 2017-11-30 14:49:20 +0100 | [diff] [blame] | 2074 | shaderStages[0].module = demo->vert_shader_module; |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2075 | shaderStages[0].pName = "main"; |
Mark Lobodzinski | 521d776 | 2015-06-23 15:11:57 -0600 | [diff] [blame] | 2076 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2077 | shaderStages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO; |
| 2078 | shaderStages[1].stage = VK_SHADER_STAGE_FRAGMENT_BIT; |
Petr Kraus | 9a4eb6a | 2017-11-30 14:49:20 +0100 | [diff] [blame] | 2079 | shaderStages[1].module = demo->frag_shader_module; |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2080 | shaderStages[1].pName = "main"; |
Mark Lobodzinski | 521d776 | 2015-06-23 15:11:57 -0600 | [diff] [blame] | 2081 | |
Jon Ashburn | c4ab7af | 2015-07-09 15:02:25 -0600 | [diff] [blame] | 2082 | memset(&pipelineCache, 0, sizeof(pipelineCache)); |
| 2083 | pipelineCache.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 2084 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 2085 | err = vkCreatePipelineCache(demo->device, &pipelineCache, NULL, &demo->pipelineCache); |
Jon Ashburn | c4ab7af | 2015-07-09 15:02:25 -0600 | [diff] [blame] | 2086 | assert(!err); |
Tony Barbour | 194bf28 | 2015-07-10 15:29:03 -0600 | [diff] [blame] | 2087 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2088 | pipeline.pVertexInputState = &vi; |
Tony Barbour | 194bf28 | 2015-07-10 15:29:03 -0600 | [diff] [blame] | 2089 | pipeline.pInputAssemblyState = &ia; |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2090 | pipeline.pRasterizationState = &rs; |
| 2091 | pipeline.pColorBlendState = &cb; |
| 2092 | pipeline.pMultisampleState = &ms; |
| 2093 | pipeline.pViewportState = &vp; |
| 2094 | pipeline.pDepthStencilState = &ds; |
Petr Kraus | 9a4eb6a | 2017-11-30 14:49:20 +0100 | [diff] [blame] | 2095 | pipeline.stageCount = ARRAY_SIZE(shaderStages); |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2096 | pipeline.pStages = shaderStages; |
| 2097 | pipeline.renderPass = demo->render_pass; |
| 2098 | pipeline.pDynamicState = &dynamicState; |
Tony Barbour | 194bf28 | 2015-07-10 15:29:03 -0600 | [diff] [blame] | 2099 | |
Courtney Goeltzenleuchter | d311fd0 | 2015-08-13 16:55:04 -0600 | [diff] [blame] | 2100 | pipeline.renderPass = demo->render_pass; |
| 2101 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 2102 | err = vkCreateGraphicsPipelines(demo->device, demo->pipelineCache, 1, &pipeline, NULL, &demo->pipeline); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 2103 | assert(!err); |
| 2104 | |
Chia-I Wu | 6363606 | 2015-10-26 21:10:41 +0800 | [diff] [blame] | 2105 | vkDestroyShaderModule(demo->device, demo->frag_shader_module, NULL); |
| 2106 | vkDestroyShaderModule(demo->device, demo->vert_shader_module, NULL); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 2107 | } |
| 2108 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2109 | static void demo_prepare_descriptor_pool(struct demo *demo) { |
Chia-I Wu | f051d26 | 2015-10-27 19:25:11 +0800 | [diff] [blame] | 2110 | const VkDescriptorPoolSize type_counts[2] = { |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 2111 | [0] = |
| 2112 | { |
| 2113 | .type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, |
| 2114 | .descriptorCount = demo->swapchainImageCount, |
| 2115 | }, |
| 2116 | [1] = |
| 2117 | { |
| 2118 | .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, |
| 2119 | .descriptorCount = demo->swapchainImageCount * DEMO_TEXTURE_COUNT, |
| 2120 | }, |
Chia-I Wu | 6a3c897 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 2121 | }; |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 2122 | const VkDescriptorPoolCreateInfo descriptor_pool = { |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 2123 | .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO, |
Chia-I Wu | 6a3c897 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 2124 | .pNext = NULL, |
Tony Barbour | a72d949 | 2017-01-11 13:35:09 -0700 | [diff] [blame] | 2125 | .maxSets = demo->swapchainImageCount, |
Chia-I Wu | f051d26 | 2015-10-27 19:25:11 +0800 | [diff] [blame] | 2126 | .poolSizeCount = 2, |
| 2127 | .pPoolSizes = type_counts, |
Chia-I Wu | 6a3c897 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 2128 | }; |
Tony Barbour | fdc2d35 | 2015-04-22 09:02:32 -0600 | [diff] [blame] | 2129 | VkResult U_ASSERT_ONLY err; |
Chia-I Wu | 6a3c897 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 2130 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 2131 | err = vkCreateDescriptorPool(demo->device, &descriptor_pool, NULL, &demo->desc_pool); |
Chia-I Wu | 6a3c897 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 2132 | assert(!err); |
| 2133 | } |
| 2134 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2135 | static void demo_prepare_descriptor_set(struct demo *demo) { |
Courtney Goeltzenleuchter | c3ad65e | 2015-10-23 13:38:14 -0600 | [diff] [blame] | 2136 | VkDescriptorImageInfo tex_descs[DEMO_TEXTURE_COUNT]; |
Chia-I Wu | ae721ba | 2015-05-25 16:27:55 +0800 | [diff] [blame] | 2137 | VkWriteDescriptorSet writes[2]; |
Tony Barbour | fdc2d35 | 2015-04-22 09:02:32 -0600 | [diff] [blame] | 2138 | VkResult U_ASSERT_ONLY err; |
Chia-I Wu | 6a3c897 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 2139 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 2140 | VkDescriptorSetAllocateInfo alloc_info = {.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO, |
| 2141 | .pNext = NULL, |
| 2142 | .descriptorPool = demo->desc_pool, |
| 2143 | .descriptorSetCount = 1, |
| 2144 | .pSetLayouts = &demo->desc_layout}; |
Tony Barbour | a72d949 | 2017-01-11 13:35:09 -0700 | [diff] [blame] | 2145 | |
| 2146 | VkDescriptorBufferInfo buffer_info; |
| 2147 | buffer_info.offset = 0; |
| 2148 | buffer_info.range = sizeof(struct vktexcube_vs_uniform); |
Chia-I Wu | 6a3c897 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 2149 | |
Chia-I Wu | ae721ba | 2015-05-25 16:27:55 +0800 | [diff] [blame] | 2150 | memset(&tex_descs, 0, sizeof(tex_descs)); |
Karl Schultz | a261546 | 2017-01-20 13:19:20 -0700 | [diff] [blame] | 2151 | for (unsigned int i = 0; i < DEMO_TEXTURE_COUNT; i++) { |
Courtney Goeltzenleuchter | c3ad65e | 2015-10-23 13:38:14 -0600 | [diff] [blame] | 2152 | tex_descs[i].sampler = demo->textures[i].sampler; |
| 2153 | tex_descs[i].imageView = demo->textures[i].view; |
Karl Schultz | e88bc84 | 2018-09-11 16:23:14 -0600 | [diff] [blame] | 2154 | tex_descs[i].imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL; |
Chia-I Wu | ae721ba | 2015-05-25 16:27:55 +0800 | [diff] [blame] | 2155 | } |
| 2156 | |
| 2157 | memset(&writes, 0, sizeof(writes)); |
| 2158 | |
| 2159 | writes[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; |
Chia-I Wu | 3dfc134 | 2015-10-26 20:48:51 +0800 | [diff] [blame] | 2160 | writes[0].descriptorCount = 1; |
Chia-I Wu | ae721ba | 2015-05-25 16:27:55 +0800 | [diff] [blame] | 2161 | writes[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; |
Tony Barbour | a72d949 | 2017-01-11 13:35:09 -0700 | [diff] [blame] | 2162 | writes[0].pBufferInfo = &buffer_info; |
Chia-I Wu | ae721ba | 2015-05-25 16:27:55 +0800 | [diff] [blame] | 2163 | |
| 2164 | writes[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; |
Chia-I Wu | a8fe78a | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 2165 | writes[1].dstBinding = 1; |
Chia-I Wu | 3dfc134 | 2015-10-26 20:48:51 +0800 | [diff] [blame] | 2166 | writes[1].descriptorCount = DEMO_TEXTURE_COUNT; |
Chia-I Wu | ae721ba | 2015-05-25 16:27:55 +0800 | [diff] [blame] | 2167 | writes[1].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER; |
Courtney Goeltzenleuchter | c3ad65e | 2015-10-23 13:38:14 -0600 | [diff] [blame] | 2168 | writes[1].pImageInfo = tex_descs; |
Chia-I Wu | ae721ba | 2015-05-25 16:27:55 +0800 | [diff] [blame] | 2169 | |
Tony Barbour | a72d949 | 2017-01-11 13:35:09 -0700 | [diff] [blame] | 2170 | for (unsigned int i = 0; i < demo->swapchainImageCount; i++) { |
| 2171 | err = vkAllocateDescriptorSets(demo->device, &alloc_info, &demo->swapchain_image_resources[i].descriptor_set); |
| 2172 | assert(!err); |
| 2173 | buffer_info.buffer = demo->swapchain_image_resources[i].uniform_buffer; |
| 2174 | writes[0].dstSet = demo->swapchain_image_resources[i].descriptor_set; |
| 2175 | writes[1].dstSet = demo->swapchain_image_resources[i].descriptor_set; |
| 2176 | vkUpdateDescriptorSets(demo->device, 2, writes, 0, NULL); |
| 2177 | } |
Chia-I Wu | 6a3c897 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 2178 | } |
| 2179 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2180 | static void demo_prepare_framebuffers(struct demo *demo) { |
Courtney Goeltzenleuchter | 41866db | 2015-09-01 17:30:39 -0600 | [diff] [blame] | 2181 | VkImageView attachments[2]; |
Cody Northrop | 2e11cad | 2015-08-04 10:47:08 -0600 | [diff] [blame] | 2182 | attachments[1] = demo->depth.view; |
| 2183 | |
Chia-I Wu | f973e32 | 2015-07-08 13:34:24 +0800 | [diff] [blame] | 2184 | const VkFramebufferCreateInfo fb_info = { |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2185 | .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO, |
| 2186 | .pNext = NULL, |
| 2187 | .renderPass = demo->render_pass, |
| 2188 | .attachmentCount = 2, |
| 2189 | .pAttachments = attachments, |
| 2190 | .width = demo->width, |
| 2191 | .height = demo->height, |
| 2192 | .layers = 1, |
Chia-I Wu | f973e32 | 2015-07-08 13:34:24 +0800 | [diff] [blame] | 2193 | }; |
| 2194 | VkResult U_ASSERT_ONLY err; |
| 2195 | uint32_t i; |
| 2196 | |
Tony Barbour | d8e504f | 2015-10-08 14:26:24 -0600 | [diff] [blame] | 2197 | for (i = 0; i < demo->swapchainImageCount; i++) { |
Tony Barbour | a72d949 | 2017-01-11 13:35:09 -0700 | [diff] [blame] | 2198 | attachments[0] = demo->swapchain_image_resources[i].view; |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 2199 | err = vkCreateFramebuffer(demo->device, &fb_info, NULL, &demo->swapchain_image_resources[i].framebuffer); |
Chia-I Wu | f973e32 | 2015-07-08 13:34:24 +0800 | [diff] [blame] | 2200 | assert(!err); |
| 2201 | } |
| 2202 | } |
| 2203 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2204 | static void demo_prepare(struct demo *demo) { |
Cody Northrop | 91e221b | 2015-07-09 18:08:32 -0600 | [diff] [blame] | 2205 | VkResult U_ASSERT_ONLY err; |
Jeremy Kniager | 602e418 | 2018-01-19 10:52:34 -0700 | [diff] [blame] | 2206 | if (demo->cmd_pool == VK_NULL_HANDLE) { |
| 2207 | const VkCommandPoolCreateInfo cmd_pool_info = { |
| 2208 | .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO, |
| 2209 | .pNext = NULL, |
| 2210 | .queueFamilyIndex = demo->graphics_queue_family_index, |
| 2211 | .flags = 0, |
| 2212 | }; |
| 2213 | err = vkCreateCommandPool(demo->device, &cmd_pool_info, NULL, &demo->cmd_pool); |
| 2214 | assert(!err); |
| 2215 | } |
Cody Northrop | 91e221b | 2015-07-09 18:08:32 -0600 | [diff] [blame] | 2216 | |
Chia-I Wu | a8fe78a | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 2217 | const VkCommandBufferAllocateInfo cmd = { |
Chia-I Wu | f48700b | 2015-11-10 16:21:09 +0800 | [diff] [blame] | 2218 | .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 2219 | .pNext = NULL, |
Chia-I Wu | a8fe78a | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 2220 | .commandPool = demo->cmd_pool, |
| 2221 | .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY, |
Jon Ashburn | 0bec67e | 2016-01-11 13:12:43 -0700 | [diff] [blame] | 2222 | .commandBufferCount = 1, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 2223 | }; |
Tony Barbour | 6db4fcb | 2016-10-19 13:41:16 -0600 | [diff] [blame] | 2224 | err = vkAllocateCommandBuffers(demo->device, &cmd, &demo->cmd); |
| 2225 | assert(!err); |
| 2226 | VkCommandBufferBeginInfo cmd_buf_info = { |
| 2227 | .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO, |
| 2228 | .pNext = NULL, |
| 2229 | .flags = 0, |
| 2230 | .pInheritanceInfo = NULL, |
| 2231 | }; |
| 2232 | err = vkBeginCommandBuffer(demo->cmd, &cmd_buf_info); |
| 2233 | assert(!err); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 2234 | |
| 2235 | demo_prepare_buffers(demo); |
Jeremy Kniager | 602e418 | 2018-01-19 10:52:34 -0700 | [diff] [blame] | 2236 | |
| 2237 | if (demo->is_minimized) { |
| 2238 | demo->prepared = false; |
| 2239 | return; |
| 2240 | } |
| 2241 | |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 2242 | demo_prepare_depth(demo); |
| 2243 | demo_prepare_textures(demo); |
Tony Barbour | a72d949 | 2017-01-11 13:35:09 -0700 | [diff] [blame] | 2244 | demo_prepare_cube_data_buffers(demo); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 2245 | |
Chia-I Wu | 6a3c897 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 2246 | demo_prepare_descriptor_layout(demo); |
Chia-I Wu | f973e32 | 2015-07-08 13:34:24 +0800 | [diff] [blame] | 2247 | demo_prepare_render_pass(demo); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 2248 | demo_prepare_pipeline(demo); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 2249 | |
Ian Elliott | a078197 | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 2250 | for (uint32_t i = 0; i < demo->swapchainImageCount; i++) { |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 2251 | err = vkAllocateCommandBuffers(demo->device, &cmd, &demo->swapchain_image_resources[i].cmd); |
Courtney Goeltzenleuchter | efc58ae | 2015-02-17 09:48:44 -0700 | [diff] [blame] | 2252 | assert(!err); |
| 2253 | } |
Chia-I Wu | 6a3c897 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 2254 | |
Tony Barbour | 22e0627 | 2016-09-07 16:07:56 -0600 | [diff] [blame] | 2255 | if (demo->separate_present_queue) { |
Karl Schultz | a261546 | 2017-01-20 13:19:20 -0700 | [diff] [blame] | 2256 | const VkCommandPoolCreateInfo present_cmd_pool_info = { |
Tony Barbour | 22e0627 | 2016-09-07 16:07:56 -0600 | [diff] [blame] | 2257 | .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO, |
| 2258 | .pNext = NULL, |
| 2259 | .queueFamilyIndex = demo->present_queue_family_index, |
| 2260 | .flags = 0, |
| 2261 | }; |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 2262 | err = vkCreateCommandPool(demo->device, &present_cmd_pool_info, NULL, &demo->present_cmd_pool); |
Tony Barbour | 22e0627 | 2016-09-07 16:07:56 -0600 | [diff] [blame] | 2263 | assert(!err); |
Karl Schultz | a261546 | 2017-01-20 13:19:20 -0700 | [diff] [blame] | 2264 | const VkCommandBufferAllocateInfo present_cmd_info = { |
Tony Barbour | 22e0627 | 2016-09-07 16:07:56 -0600 | [diff] [blame] | 2265 | .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, |
| 2266 | .pNext = NULL, |
| 2267 | .commandPool = demo->present_cmd_pool, |
| 2268 | .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY, |
| 2269 | .commandBufferCount = 1, |
| 2270 | }; |
| 2271 | for (uint32_t i = 0; i < demo->swapchainImageCount; i++) { |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 2272 | err = vkAllocateCommandBuffers(demo->device, &present_cmd_info, |
| 2273 | &demo->swapchain_image_resources[i].graphics_to_present_cmd); |
Tony Barbour | 22e0627 | 2016-09-07 16:07:56 -0600 | [diff] [blame] | 2274 | assert(!err); |
| 2275 | demo_build_image_ownership_cmd(demo, i); |
| 2276 | } |
| 2277 | } |
| 2278 | |
Chia-I Wu | 63ea926 | 2015-03-26 13:14:16 +0800 | [diff] [blame] | 2279 | demo_prepare_descriptor_pool(demo); |
Chia-I Wu | 6a3c897 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 2280 | demo_prepare_descriptor_set(demo); |
Courtney Goeltzenleuchter | efc58ae | 2015-02-17 09:48:44 -0700 | [diff] [blame] | 2281 | |
Chia-I Wu | f973e32 | 2015-07-08 13:34:24 +0800 | [diff] [blame] | 2282 | demo_prepare_framebuffers(demo); |
Courtney Goeltzenleuchter | efc58ae | 2015-02-17 09:48:44 -0700 | [diff] [blame] | 2283 | |
Ian Elliott | a078197 | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 2284 | for (uint32_t i = 0; i < demo->swapchainImageCount; i++) { |
Courtney Goeltzenleuchter | efc58ae | 2015-02-17 09:48:44 -0700 | [diff] [blame] | 2285 | demo->current_buffer = i; |
Tony Barbour | a72d949 | 2017-01-11 13:35:09 -0700 | [diff] [blame] | 2286 | demo_draw_build_cmd(demo, demo->swapchain_image_resources[i].cmd); |
Courtney Goeltzenleuchter | efc58ae | 2015-02-17 09:48:44 -0700 | [diff] [blame] | 2287 | } |
| 2288 | |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 2289 | /* |
| 2290 | * Prepare functions above may generate pipeline commands |
| 2291 | * that need to be flushed before beginning the render loop. |
| 2292 | */ |
| 2293 | demo_flush_init_cmd(demo); |
Tony-LunarG | bc9fc05 | 2018-09-21 13:47:06 -0600 | [diff] [blame] | 2294 | if (demo->staging_texture.buffer) { |
| 2295 | demo_destroy_texture(demo, &demo->staging_texture); |
Tony Barbour | 16156cb | 2016-10-19 14:15:49 -0600 | [diff] [blame] | 2296 | } |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 2297 | |
Courtney Goeltzenleuchter | efc58ae | 2015-02-17 09:48:44 -0700 | [diff] [blame] | 2298 | demo->current_buffer = 0; |
Mike Stroyan | bb60b38 | 2015-10-28 09:46:57 -0600 | [diff] [blame] | 2299 | demo->prepared = true; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 2300 | } |
| 2301 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2302 | static void demo_cleanup(struct demo *demo) { |
David Pinedo | 2bb7c93 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 2303 | uint32_t i; |
| 2304 | |
| 2305 | demo->prepared = false; |
Tony Barbour | ce7524e | 2016-07-28 12:01:45 -0600 | [diff] [blame] | 2306 | vkDeviceWaitIdle(demo->device); |
David Pinedo | 2bb7c93 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 2307 | |
Tony Barbour | 66a56c3 | 2016-09-21 13:10:56 -0600 | [diff] [blame] | 2308 | // Wait for fences from present operations |
| 2309 | for (i = 0; i < FRAME_LAG; i++) { |
szdarkhack | d322ff0 | 2016-10-08 09:51:22 +0300 | [diff] [blame] | 2310 | vkWaitForFences(demo->device, 1, &demo->fences[i], VK_TRUE, UINT64_MAX); |
Tony Barbour | 66a56c3 | 2016-09-21 13:10:56 -0600 | [diff] [blame] | 2311 | vkDestroyFence(demo->device, demo->fences[i], NULL); |
| 2312 | vkDestroySemaphore(demo->device, demo->image_acquired_semaphores[i], NULL); |
| 2313 | vkDestroySemaphore(demo->device, demo->draw_complete_semaphores[i], NULL); |
| 2314 | if (demo->separate_present_queue) { |
| 2315 | vkDestroySemaphore(demo->device, demo->image_ownership_semaphores[i], NULL); |
| 2316 | } |
| 2317 | } |
| 2318 | |
Jeremy Kniager | 602e418 | 2018-01-19 10:52:34 -0700 | [diff] [blame] | 2319 | // If the window is currently minimized, demo_resize has already done some cleanup for us. |
| 2320 | if (!demo->is_minimized) { |
| 2321 | for (i = 0; i < demo->swapchainImageCount; i++) { |
| 2322 | vkDestroyFramebuffer(demo->device, demo->swapchain_image_resources[i].framebuffer, NULL); |
| 2323 | } |
| 2324 | vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL); |
Chia-I Wu | f973e32 | 2015-07-08 13:34:24 +0800 | [diff] [blame] | 2325 | |
Jeremy Kniager | 602e418 | 2018-01-19 10:52:34 -0700 | [diff] [blame] | 2326 | vkDestroyPipeline(demo->device, demo->pipeline, NULL); |
| 2327 | vkDestroyPipelineCache(demo->device, demo->pipelineCache, NULL); |
| 2328 | vkDestroyRenderPass(demo->device, demo->render_pass, NULL); |
| 2329 | vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL); |
| 2330 | vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL); |
David Pinedo | 2bb7c93 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 2331 | |
Jeremy Kniager | 602e418 | 2018-01-19 10:52:34 -0700 | [diff] [blame] | 2332 | for (i = 0; i < DEMO_TEXTURE_COUNT; i++) { |
| 2333 | vkDestroyImageView(demo->device, demo->textures[i].view, NULL); |
| 2334 | vkDestroyImage(demo->device, demo->textures[i].image, NULL); |
| 2335 | vkFreeMemory(demo->device, demo->textures[i].mem, NULL); |
| 2336 | vkDestroySampler(demo->device, demo->textures[i].sampler, NULL); |
| 2337 | } |
| 2338 | demo->fpDestroySwapchainKHR(demo->device, demo->swapchain, NULL); |
David Pinedo | 2bb7c93 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 2339 | |
Jeremy Kniager | 602e418 | 2018-01-19 10:52:34 -0700 | [diff] [blame] | 2340 | vkDestroyImageView(demo->device, demo->depth.view, NULL); |
| 2341 | vkDestroyImage(demo->device, demo->depth.image, NULL); |
| 2342 | vkFreeMemory(demo->device, demo->depth.mem, NULL); |
David Pinedo | 2bb7c93 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 2343 | |
Jeremy Kniager | 602e418 | 2018-01-19 10:52:34 -0700 | [diff] [blame] | 2344 | for (i = 0; i < demo->swapchainImageCount; i++) { |
| 2345 | vkDestroyImageView(demo->device, demo->swapchain_image_resources[i].view, NULL); |
| 2346 | vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, &demo->swapchain_image_resources[i].cmd); |
| 2347 | vkDestroyBuffer(demo->device, demo->swapchain_image_resources[i].uniform_buffer, NULL); |
Tony-LunarG | 37af49f | 2019-12-19 12:04:19 -0700 | [diff] [blame] | 2348 | vkUnmapMemory(demo->device, demo->swapchain_image_resources[i].uniform_memory); |
Jeremy Kniager | 602e418 | 2018-01-19 10:52:34 -0700 | [diff] [blame] | 2349 | vkFreeMemory(demo->device, demo->swapchain_image_resources[i].uniform_memory, NULL); |
| 2350 | } |
| 2351 | free(demo->swapchain_image_resources); |
| 2352 | free(demo->queue_props); |
| 2353 | vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL); |
Tony Barbour | 66a56c3 | 2016-09-21 13:10:56 -0600 | [diff] [blame] | 2354 | |
Jeremy Kniager | 602e418 | 2018-01-19 10:52:34 -0700 | [diff] [blame] | 2355 | if (demo->separate_present_queue) { |
| 2356 | vkDestroyCommandPool(demo->device, demo->present_cmd_pool, NULL); |
| 2357 | } |
Tony Barbour | 22e0627 | 2016-09-07 16:07:56 -0600 | [diff] [blame] | 2358 | } |
Tony Barbour | ffa9a42 | 2016-11-10 16:45:15 -0700 | [diff] [blame] | 2359 | vkDeviceWaitIdle(demo->device); |
Chia-I Wu | 6363606 | 2015-10-26 21:10:41 +0800 | [diff] [blame] | 2360 | vkDestroyDevice(demo->device, NULL); |
Tony Barbour | d70b42c | 2015-06-30 14:14:19 -0600 | [diff] [blame] | 2361 | if (demo->validate) { |
Mark Young | 66510e5 | 2017-11-10 10:05:14 -0700 | [diff] [blame] | 2362 | demo->DestroyDebugUtilsMessengerEXT(demo->inst, demo->dbg_messenger, NULL); |
Tony Barbour | d70b42c | 2015-06-30 14:14:19 -0600 | [diff] [blame] | 2363 | } |
Ian Elliott | b08e0d9 | 2015-11-20 11:55:46 -0700 | [diff] [blame] | 2364 | vkDestroySurfaceKHR(demo->inst, demo->surface, NULL); |
David Pinedo | 2bb7c93 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 2365 | |
Tony Barbour | 153cb06 | 2016-12-07 13:43:36 -0700 | [diff] [blame] | 2366 | #if defined(VK_USE_PLATFORM_XLIB_KHR) |
Tony Barbour | 78d6b57 | 2016-11-14 14:46:33 -0700 | [diff] [blame] | 2367 | XDestroyWindow(demo->display, demo->xlib_window); |
| 2368 | XCloseDisplay(demo->display); |
Michael Lentine | c6cde4b | 2016-04-18 13:20:45 -0500 | [diff] [blame] | 2369 | #elif defined(VK_USE_PLATFORM_XCB_KHR) |
Pavol Klacansky | 573a19d | 2016-05-10 03:08:19 -0600 | [diff] [blame] | 2370 | xcb_destroy_window(demo->connection, demo->xcb_window); |
Michael Lentine | c6cde4b | 2016-04-18 13:20:45 -0500 | [diff] [blame] | 2371 | xcb_disconnect(demo->connection); |
| 2372 | free(demo->atom_wm_delete_window); |
Mun Gwan-gyeong | 6f01c55 | 2016-06-27 05:34:44 +0900 | [diff] [blame] | 2373 | #elif defined(VK_USE_PLATFORM_WAYLAND_KHR) |
Joey Bzdek | 15eb070 | 2017-06-07 09:40:36 -0600 | [diff] [blame] | 2374 | wl_keyboard_destroy(demo->keyboard); |
| 2375 | wl_pointer_destroy(demo->pointer); |
| 2376 | wl_seat_destroy(demo->seat); |
Tony-LunarG | 5ceb7be | 2019-12-05 16:05:40 -0700 | [diff] [blame] | 2377 | wl_shell_surface_destroy(demo->shell_surface); |
Mun Gwan-gyeong | 6f01c55 | 2016-06-27 05:34:44 +0900 | [diff] [blame] | 2378 | wl_surface_destroy(demo->window); |
Tony-LunarG | 5ceb7be | 2019-12-05 16:05:40 -0700 | [diff] [blame] | 2379 | wl_shell_destroy(demo->shell); |
Mun Gwan-gyeong | 6f01c55 | 2016-06-27 05:34:44 +0900 | [diff] [blame] | 2380 | wl_compositor_destroy(demo->compositor); |
| 2381 | wl_registry_destroy(demo->registry); |
| 2382 | wl_display_disconnect(demo->display); |
Nicolas Caramelli | c8be8c8 | 2020-07-13 17:22:09 +0200 | [diff] [blame] | 2383 | #elif defined(VK_USE_PLATFORM_DIRECTFB_EXT) |
| 2384 | demo->event_buffer->Release(demo->event_buffer); |
| 2385 | demo->window->Release(demo->window); |
| 2386 | demo->dfb->Release(demo->dfb); |
Michael Lentine | c6cde4b | 2016-04-18 13:20:45 -0500 | [diff] [blame] | 2387 | #endif |
Karl Schultz | 8642911 | 2017-06-20 11:27:59 -0600 | [diff] [blame] | 2388 | |
| 2389 | vkDestroyInstance(demo->inst, NULL); |
David Pinedo | 2bb7c93 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 2390 | } |
| 2391 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2392 | static void demo_resize(struct demo *demo) { |
Ian Elliott | cf074d3 | 2015-10-16 18:02:43 -0600 | [diff] [blame] | 2393 | uint32_t i; |
| 2394 | |
Mike Stroyan | bb60b38 | 2015-10-28 09:46:57 -0600 | [diff] [blame] | 2395 | // Don't react to resize until after first initialization. |
| 2396 | if (!demo->prepared) { |
Jeremy Kniager | 602e418 | 2018-01-19 10:52:34 -0700 | [diff] [blame] | 2397 | if (demo->is_minimized) { |
| 2398 | demo_prepare(demo); |
| 2399 | } |
Mike Stroyan | bb60b38 | 2015-10-28 09:46:57 -0600 | [diff] [blame] | 2400 | return; |
| 2401 | } |
Ian Elliott | cf074d3 | 2015-10-16 18:02:43 -0600 | [diff] [blame] | 2402 | // In order to properly resize the window, we must re-create the swapchain |
| 2403 | // AND redo the command buffers, etc. |
| 2404 | // |
| 2405 | // First, perform part of the demo_cleanup() function: |
| 2406 | demo->prepared = false; |
Tony Barbour | ce7524e | 2016-07-28 12:01:45 -0600 | [diff] [blame] | 2407 | vkDeviceWaitIdle(demo->device); |
Ian Elliott | cf074d3 | 2015-10-16 18:02:43 -0600 | [diff] [blame] | 2408 | |
| 2409 | for (i = 0; i < demo->swapchainImageCount; i++) { |
Tony Barbour | a72d949 | 2017-01-11 13:35:09 -0700 | [diff] [blame] | 2410 | vkDestroyFramebuffer(demo->device, demo->swapchain_image_resources[i].framebuffer, NULL); |
Ian Elliott | cf074d3 | 2015-10-16 18:02:43 -0600 | [diff] [blame] | 2411 | } |
Chia-I Wu | 6363606 | 2015-10-26 21:10:41 +0800 | [diff] [blame] | 2412 | vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL); |
Ian Elliott | cf074d3 | 2015-10-16 18:02:43 -0600 | [diff] [blame] | 2413 | |
Chia-I Wu | 6363606 | 2015-10-26 21:10:41 +0800 | [diff] [blame] | 2414 | vkDestroyPipeline(demo->device, demo->pipeline, NULL); |
| 2415 | vkDestroyPipelineCache(demo->device, demo->pipelineCache, NULL); |
| 2416 | vkDestroyRenderPass(demo->device, demo->render_pass, NULL); |
| 2417 | vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL); |
| 2418 | vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL); |
Ian Elliott | cf074d3 | 2015-10-16 18:02:43 -0600 | [diff] [blame] | 2419 | |
| 2420 | for (i = 0; i < DEMO_TEXTURE_COUNT; i++) { |
Chia-I Wu | 6363606 | 2015-10-26 21:10:41 +0800 | [diff] [blame] | 2421 | vkDestroyImageView(demo->device, demo->textures[i].view, NULL); |
| 2422 | vkDestroyImage(demo->device, demo->textures[i].image, NULL); |
| 2423 | vkFreeMemory(demo->device, demo->textures[i].mem, NULL); |
| 2424 | vkDestroySampler(demo->device, demo->textures[i].sampler, NULL); |
Ian Elliott | cf074d3 | 2015-10-16 18:02:43 -0600 | [diff] [blame] | 2425 | } |
Ian Elliott | cf074d3 | 2015-10-16 18:02:43 -0600 | [diff] [blame] | 2426 | |
Chia-I Wu | 6363606 | 2015-10-26 21:10:41 +0800 | [diff] [blame] | 2427 | vkDestroyImageView(demo->device, demo->depth.view, NULL); |
| 2428 | vkDestroyImage(demo->device, demo->depth.image, NULL); |
| 2429 | vkFreeMemory(demo->device, demo->depth.mem, NULL); |
Ian Elliott | cf074d3 | 2015-10-16 18:02:43 -0600 | [diff] [blame] | 2430 | |
Ian Elliott | cf074d3 | 2015-10-16 18:02:43 -0600 | [diff] [blame] | 2431 | for (i = 0; i < demo->swapchainImageCount; i++) { |
Tony Barbour | a72d949 | 2017-01-11 13:35:09 -0700 | [diff] [blame] | 2432 | vkDestroyImageView(demo->device, demo->swapchain_image_resources[i].view, NULL); |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 2433 | vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, &demo->swapchain_image_resources[i].cmd); |
Tony Barbour | a72d949 | 2017-01-11 13:35:09 -0700 | [diff] [blame] | 2434 | vkDestroyBuffer(demo->device, demo->swapchain_image_resources[i].uniform_buffer, NULL); |
Tony-LunarG | 37af49f | 2019-12-19 12:04:19 -0700 | [diff] [blame] | 2435 | vkUnmapMemory(demo->device, demo->swapchain_image_resources[i].uniform_memory); |
Tony Barbour | a72d949 | 2017-01-11 13:35:09 -0700 | [diff] [blame] | 2436 | vkFreeMemory(demo->device, demo->swapchain_image_resources[i].uniform_memory, NULL); |
Ian Elliott | cf074d3 | 2015-10-16 18:02:43 -0600 | [diff] [blame] | 2437 | } |
Chia-I Wu | 6363606 | 2015-10-26 21:10:41 +0800 | [diff] [blame] | 2438 | vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL); |
Jeremy Kniager | 602e418 | 2018-01-19 10:52:34 -0700 | [diff] [blame] | 2439 | demo->cmd_pool = VK_NULL_HANDLE; |
Tony Barbour | 22e0627 | 2016-09-07 16:07:56 -0600 | [diff] [blame] | 2440 | if (demo->separate_present_queue) { |
| 2441 | vkDestroyCommandPool(demo->device, demo->present_cmd_pool, NULL); |
| 2442 | } |
Tony Barbour | a72d949 | 2017-01-11 13:35:09 -0700 | [diff] [blame] | 2443 | free(demo->swapchain_image_resources); |
Ian Elliott | cf074d3 | 2015-10-16 18:02:43 -0600 | [diff] [blame] | 2444 | |
Ian Elliott | cf074d3 | 2015-10-16 18:02:43 -0600 | [diff] [blame] | 2445 | // Second, re-perform the demo_prepare() function, which will re-create the |
| 2446 | // swapchain: |
| 2447 | demo_prepare(demo); |
| 2448 | } |
| 2449 | |
David Pinedo | 2bb7c93 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 2450 | // On MS-Windows, make this a global, so it's available to WndProc() |
| 2451 | struct demo demo; |
| 2452 | |
Michael Lentine | c6cde4b | 2016-04-18 13:20:45 -0500 | [diff] [blame] | 2453 | #if defined(VK_USE_PLATFORM_WIN32_KHR) |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2454 | static void demo_run(struct demo *demo) { |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 2455 | if (!demo->prepared) return; |
Tony Barbour | ce7524e | 2016-07-28 12:01:45 -0600 | [diff] [blame] | 2456 | |
Ian Elliott | 639ca47 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 2457 | demo_draw(demo); |
David Pinedo | 2bb7c93 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 2458 | demo->curFrame++; |
Petr Kraus | d88e4e5 | 2019-01-23 18:45:04 +0100 | [diff] [blame] | 2459 | if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount) { |
Karl Schultz | 8a66391 | 2016-03-24 18:43:41 -0600 | [diff] [blame] | 2460 | PostQuitMessage(validation_error); |
David Pinedo | 2bb7c93 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 2461 | } |
David Pinedo | 2bb7c93 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 2462 | } |
Ian Elliott | 639ca47 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 2463 | |
| 2464 | // MS-Windows event handling function: |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2465 | LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { |
| 2466 | switch (uMsg) { |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 2467 | case WM_CLOSE: |
| 2468 | PostQuitMessage(validation_error); |
| 2469 | break; |
| 2470 | case WM_PAINT: |
| 2471 | // The validation callback calls MessageBox which can generate paint |
| 2472 | // events - don't make more Vulkan calls if we got here from the |
| 2473 | // callback |
| 2474 | if (!in_callback) { |
| 2475 | demo_run(&demo); |
| 2476 | } |
| 2477 | break; |
| 2478 | case WM_GETMINMAXINFO: // set window's minimum size |
| 2479 | ((MINMAXINFO *)lParam)->ptMinTrackSize = demo.minsize; |
| 2480 | return 0; |
Aaron Hagan | 500b9c3 | 2018-10-03 21:56:29 -0400 | [diff] [blame] | 2481 | case WM_ERASEBKGND: |
| 2482 | return 1; |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 2483 | case WM_SIZE: |
| 2484 | // Resize the application to the new window size, except when |
| 2485 | // it was minimized. Vulkan doesn't support images or swapchains |
| 2486 | // with width=0 and height=0. |
| 2487 | if (wParam != SIZE_MINIMIZED) { |
| 2488 | demo.width = lParam & 0xffff; |
| 2489 | demo.height = (lParam & 0xffff0000) >> 16; |
| 2490 | demo_resize(&demo); |
| 2491 | } |
| 2492 | break; |
Petr Kraus | d88e4e5 | 2019-01-23 18:45:04 +0100 | [diff] [blame] | 2493 | case WM_KEYDOWN: |
| 2494 | switch (wParam) { |
| 2495 | case VK_ESCAPE: |
| 2496 | PostQuitMessage(validation_error); |
| 2497 | break; |
| 2498 | case VK_LEFT: |
| 2499 | demo.spin_angle -= demo.spin_increment; |
| 2500 | break; |
| 2501 | case VK_RIGHT: |
| 2502 | demo.spin_angle += demo.spin_increment; |
| 2503 | break; |
| 2504 | case VK_SPACE: |
| 2505 | demo.pause = !demo.pause; |
| 2506 | break; |
| 2507 | } |
| 2508 | return 0; |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 2509 | default: |
| 2510 | break; |
Ian Elliott | 639ca47 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 2511 | } |
| 2512 | return (DefWindowProc(hWnd, uMsg, wParam, lParam)); |
| 2513 | } |
| 2514 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2515 | static void demo_create_window(struct demo *demo) { |
| 2516 | WNDCLASSEX win_class; |
Ian Elliott | 639ca47 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 2517 | |
| 2518 | // Initialize the window class structure: |
| 2519 | win_class.cbSize = sizeof(WNDCLASSEX); |
| 2520 | win_class.style = CS_HREDRAW | CS_VREDRAW; |
| 2521 | win_class.lpfnWndProc = WndProc; |
| 2522 | win_class.cbClsExtra = 0; |
| 2523 | win_class.cbWndExtra = 0; |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 2524 | win_class.hInstance = demo->connection; // hInstance |
Ian Elliott | 639ca47 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 2525 | win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION); |
| 2526 | win_class.hCursor = LoadCursor(NULL, IDC_ARROW); |
| 2527 | win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); |
| 2528 | win_class.lpszMenuName = NULL; |
| 2529 | win_class.lpszClassName = demo->name; |
| 2530 | win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO); |
| 2531 | // Register window class: |
| 2532 | if (!RegisterClassEx(&win_class)) { |
| 2533 | // It didn't work, so try to give a useful error: |
| 2534 | printf("Unexpected error trying to start the application!\n"); |
| 2535 | fflush(stdout); |
| 2536 | exit(1); |
| 2537 | } |
| 2538 | // Create window with the registered class: |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2539 | RECT wr = {0, 0, demo->width, demo->height}; |
Mike Stroyan | b46779e | 2015-06-15 14:20:13 -0600 | [diff] [blame] | 2540 | AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE); |
Ian Elliott | 639ca47 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 2541 | demo->window = CreateWindowEx(0, |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 2542 | demo->name, // class name |
| 2543 | demo->name, // app name |
| 2544 | WS_OVERLAPPEDWINDOW | // window style |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2545 | WS_VISIBLE | WS_SYSMENU, |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 2546 | 100, 100, // x/y coords |
| 2547 | wr.right - wr.left, // width |
| 2548 | wr.bottom - wr.top, // height |
| 2549 | NULL, // handle to parent |
| 2550 | NULL, // handle to menu |
| 2551 | demo->connection, // hInstance |
| 2552 | NULL); // no extra parameters |
Ian Elliott | 639ca47 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 2553 | if (!demo->window) { |
| 2554 | // It didn't work, so try to give a useful error: |
| 2555 | printf("Cannot create a window in which to draw!\n"); |
| 2556 | fflush(stdout); |
| 2557 | exit(1); |
| 2558 | } |
Rene Lindsay | b9403da | 2016-07-01 18:00:30 -0600 | [diff] [blame] | 2559 | // Window client area size must be at least 1 pixel high, to prevent crash. |
| 2560 | demo->minsize.x = GetSystemMetrics(SM_CXMINTRACK); |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 2561 | demo->minsize.y = GetSystemMetrics(SM_CYMINTRACK) + 1; |
Ian Elliott | 639ca47 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 2562 | } |
Tony Barbour | 8295ac4 | 2016-08-08 11:04:17 -0600 | [diff] [blame] | 2563 | #elif defined(VK_USE_PLATFORM_XLIB_KHR) |
Tony Barbour | 2593b18 | 2016-04-19 10:57:58 -0600 | [diff] [blame] | 2564 | static void demo_create_xlib_window(struct demo *demo) { |
Mike Weiblen | 5d2ad54 | 2018-02-13 13:56:13 -0700 | [diff] [blame] | 2565 | const char *display_envar = getenv("DISPLAY"); |
| 2566 | if (display_envar == NULL || display_envar[0] == '\0') { |
| 2567 | printf("Environment variable DISPLAY requires a valid value.\nExiting ...\n"); |
| 2568 | fflush(stdout); |
| 2569 | exit(1); |
| 2570 | } |
Tony Barbour | 2593b18 | 2016-04-19 10:57:58 -0600 | [diff] [blame] | 2571 | |
Tony Barbour | ee9cd37 | 2017-01-31 16:09:58 -0700 | [diff] [blame] | 2572 | XInitThreads(); |
Tony Barbour | 2593b18 | 2016-04-19 10:57:58 -0600 | [diff] [blame] | 2573 | demo->display = XOpenDisplay(NULL); |
| 2574 | long visualMask = VisualScreenMask; |
| 2575 | int numberOfVisuals; |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 2576 | XVisualInfo vInfoTemplate = {}; |
Tony Barbour | 2593b18 | 2016-04-19 10:57:58 -0600 | [diff] [blame] | 2577 | vInfoTemplate.screen = DefaultScreen(demo->display); |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 2578 | XVisualInfo *visualInfo = XGetVisualInfo(demo->display, visualMask, &vInfoTemplate, &numberOfVisuals); |
Tony Barbour | 2593b18 | 2016-04-19 10:57:58 -0600 | [diff] [blame] | 2579 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 2580 | Colormap colormap = |
| 2581 | XCreateColormap(demo->display, RootWindow(demo->display, vInfoTemplate.screen), visualInfo->visual, AllocNone); |
Tony Barbour | 2593b18 | 2016-04-19 10:57:58 -0600 | [diff] [blame] | 2582 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 2583 | XSetWindowAttributes windowAttributes = {}; |
Tony Barbour | 2593b18 | 2016-04-19 10:57:58 -0600 | [diff] [blame] | 2584 | windowAttributes.colormap = colormap; |
| 2585 | windowAttributes.background_pixel = 0xFFFFFFFF; |
| 2586 | windowAttributes.border_pixel = 0; |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 2587 | windowAttributes.event_mask = KeyPressMask | KeyReleaseMask | StructureNotifyMask | ExposureMask; |
Tony Barbour | 2593b18 | 2016-04-19 10:57:58 -0600 | [diff] [blame] | 2588 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 2589 | demo->xlib_window = XCreateWindow(demo->display, RootWindow(demo->display, vInfoTemplate.screen), 0, 0, demo->width, |
| 2590 | demo->height, 0, visualInfo->depth, InputOutput, visualInfo->visual, |
| 2591 | CWBackPixel | CWBorderPixel | CWEventMask | CWColormap, &windowAttributes); |
Tony Barbour | 2593b18 | 2016-04-19 10:57:58 -0600 | [diff] [blame] | 2592 | |
| 2593 | XSelectInput(demo->display, demo->xlib_window, ExposureMask | KeyPressMask); |
| 2594 | XMapWindow(demo->display, demo->xlib_window); |
| 2595 | XFlush(demo->display); |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 2596 | demo->xlib_wm_delete_window = XInternAtom(demo->display, "WM_DELETE_WINDOW", False); |
Tony Barbour | 2593b18 | 2016-04-19 10:57:58 -0600 | [diff] [blame] | 2597 | } |
| 2598 | static void demo_handle_xlib_event(struct demo *demo, const XEvent *event) { |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 2599 | switch (event->type) { |
| 2600 | case ClientMessage: |
| 2601 | if ((Atom)event->xclient.data.l[0] == demo->xlib_wm_delete_window) demo->quit = true; |
Tony Barbour | 2593b18 | 2016-04-19 10:57:58 -0600 | [diff] [blame] | 2602 | break; |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 2603 | case KeyPress: |
| 2604 | switch (event->xkey.keycode) { |
| 2605 | case 0x9: // Escape |
| 2606 | demo->quit = true; |
| 2607 | break; |
| 2608 | case 0x71: // left arrow key |
| 2609 | demo->spin_angle -= demo->spin_increment; |
| 2610 | break; |
| 2611 | case 0x72: // right arrow key |
| 2612 | demo->spin_angle += demo->spin_increment; |
| 2613 | break; |
| 2614 | case 0x41: // space bar |
| 2615 | demo->pause = !demo->pause; |
| 2616 | break; |
| 2617 | } |
Tony Barbour | 2593b18 | 2016-04-19 10:57:58 -0600 | [diff] [blame] | 2618 | break; |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 2619 | case ConfigureNotify: |
| 2620 | if ((demo->width != event->xconfigure.width) || (demo->height != event->xconfigure.height)) { |
| 2621 | demo->width = event->xconfigure.width; |
| 2622 | demo->height = event->xconfigure.height; |
| 2623 | demo_resize(demo); |
| 2624 | } |
Karl Schultz | 6ddf1e0 | 2017-01-04 10:31:20 -0700 | [diff] [blame] | 2625 | break; |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 2626 | default: |
Tony Barbour | 2593b18 | 2016-04-19 10:57:58 -0600 | [diff] [blame] | 2627 | break; |
Tony Barbour | 2593b18 | 2016-04-19 10:57:58 -0600 | [diff] [blame] | 2628 | } |
Tony Barbour | 2593b18 | 2016-04-19 10:57:58 -0600 | [diff] [blame] | 2629 | } |
| 2630 | |
| 2631 | static void demo_run_xlib(struct demo *demo) { |
Tony Barbour | 2593b18 | 2016-04-19 10:57:58 -0600 | [diff] [blame] | 2632 | while (!demo->quit) { |
| 2633 | XEvent event; |
| 2634 | |
| 2635 | if (demo->pause) { |
| 2636 | XNextEvent(demo->display, &event); |
| 2637 | demo_handle_xlib_event(demo, &event); |
Karl Schultz | 6ddf1e0 | 2017-01-04 10:31:20 -0700 | [diff] [blame] | 2638 | } |
| 2639 | while (XPending(demo->display) > 0) { |
| 2640 | XNextEvent(demo->display, &event); |
| 2641 | demo_handle_xlib_event(demo, &event); |
Tony Barbour | 2593b18 | 2016-04-19 10:57:58 -0600 | [diff] [blame] | 2642 | } |
| 2643 | |
Tony Barbour | 2593b18 | 2016-04-19 10:57:58 -0600 | [diff] [blame] | 2644 | demo_draw(demo); |
Tony Barbour | 2593b18 | 2016-04-19 10:57:58 -0600 | [diff] [blame] | 2645 | demo->curFrame++; |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 2646 | if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount) demo->quit = true; |
Tony Barbour | 2593b18 | 2016-04-19 10:57:58 -0600 | [diff] [blame] | 2647 | } |
| 2648 | } |
Tony Barbour | 153cb06 | 2016-12-07 13:43:36 -0700 | [diff] [blame] | 2649 | #elif defined(VK_USE_PLATFORM_XCB_KHR) |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 2650 | static void demo_handle_xcb_event(struct demo *demo, const xcb_generic_event_t *event) { |
Piers Daniell | 735ee53 | 2015-02-23 16:23:13 -0700 | [diff] [blame] | 2651 | uint8_t event_code = event->response_type & 0x7f; |
Courtney Goeltzenleuchter | 98cb2cb | 2014-11-06 14:27:52 -0700 | [diff] [blame] | 2652 | switch (event_code) { |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 2653 | case XCB_EXPOSE: |
| 2654 | // TODO: Resize window |
| 2655 | break; |
| 2656 | case XCB_CLIENT_MESSAGE: |
| 2657 | if ((*(xcb_client_message_event_t *)event).data.data32[0] == (*demo->atom_wm_delete_window).atom) { |
| 2658 | demo->quit = true; |
| 2659 | } |
| 2660 | break; |
| 2661 | case XCB_KEY_RELEASE: { |
| 2662 | const xcb_key_release_event_t *key = (const xcb_key_release_event_t *)event; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 2663 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 2664 | switch (key->detail) { |
| 2665 | case 0x9: // Escape |
| 2666 | demo->quit = true; |
| 2667 | break; |
| 2668 | case 0x71: // left arrow key |
| 2669 | demo->spin_angle -= demo->spin_increment; |
| 2670 | break; |
| 2671 | case 0x72: // right arrow key |
| 2672 | demo->spin_angle += demo->spin_increment; |
| 2673 | break; |
| 2674 | case 0x41: // space bar |
| 2675 | demo->pause = !demo->pause; |
| 2676 | break; |
| 2677 | } |
| 2678 | } break; |
| 2679 | case XCB_CONFIGURE_NOTIFY: { |
| 2680 | const xcb_configure_notify_event_t *cfg = (const xcb_configure_notify_event_t *)event; |
| 2681 | if ((demo->width != cfg->width) || (demo->height != cfg->height)) { |
| 2682 | demo->width = cfg->width; |
| 2683 | demo->height = cfg->height; |
| 2684 | demo_resize(demo); |
| 2685 | } |
| 2686 | } break; |
| 2687 | default: |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2688 | break; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 2689 | } |
| 2690 | } |
| 2691 | |
Tony Barbour | 2593b18 | 2016-04-19 10:57:58 -0600 | [diff] [blame] | 2692 | static void demo_run_xcb(struct demo *demo) { |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 2693 | xcb_flush(demo->connection); |
| 2694 | |
| 2695 | while (!demo->quit) { |
| 2696 | xcb_generic_event_t *event; |
| 2697 | |
Courtney Goeltzenleuchter | c465d6c | 2014-11-18 11:28:09 -0700 | [diff] [blame] | 2698 | if (demo->pause) { |
| 2699 | event = xcb_wait_for_event(demo->connection); |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 2700 | } else { |
Courtney Goeltzenleuchter | c465d6c | 2014-11-18 11:28:09 -0700 | [diff] [blame] | 2701 | event = xcb_poll_for_event(demo->connection); |
Karl Schultz | 6ddf1e0 | 2017-01-04 10:31:20 -0700 | [diff] [blame] | 2702 | } |
| 2703 | while (event) { |
| 2704 | demo_handle_xcb_event(demo, event); |
| 2705 | free(event); |
| 2706 | event = xcb_poll_for_event(demo->connection); |
Courtney Goeltzenleuchter | 9808555 | 2014-11-10 11:13:13 -0700 | [diff] [blame] | 2707 | } |
Courtney Goeltzenleuchter | c465d6c | 2014-11-18 11:28:09 -0700 | [diff] [blame] | 2708 | |
Courtney Goeltzenleuchter | 9808555 | 2014-11-10 11:13:13 -0700 | [diff] [blame] | 2709 | demo_draw(demo); |
David Pinedo | 2bb7c93 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 2710 | demo->curFrame++; |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 2711 | if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount) demo->quit = true; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 2712 | } |
| 2713 | } |
| 2714 | |
Tony Barbour | 2593b18 | 2016-04-19 10:57:58 -0600 | [diff] [blame] | 2715 | static void demo_create_xcb_window(struct demo *demo) { |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 2716 | uint32_t value_mask, value_list[32]; |
| 2717 | |
Tony Barbour | 2593b18 | 2016-04-19 10:57:58 -0600 | [diff] [blame] | 2718 | demo->xcb_window = xcb_generate_id(demo->connection); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 2719 | |
| 2720 | value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK; |
| 2721 | value_list[0] = demo->screen->black_pixel; |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 2722 | value_list[1] = XCB_EVENT_MASK_KEY_RELEASE | XCB_EVENT_MASK_EXPOSURE | XCB_EVENT_MASK_STRUCTURE_NOTIFY; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 2723 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 2724 | xcb_create_window(demo->connection, XCB_COPY_FROM_PARENT, demo->xcb_window, demo->screen->root, 0, 0, demo->width, demo->height, |
| 2725 | 0, XCB_WINDOW_CLASS_INPUT_OUTPUT, demo->screen->root_visual, value_mask, value_list); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 2726 | |
Courtney Goeltzenleuchter | 98cb2cb | 2014-11-06 14:27:52 -0700 | [diff] [blame] | 2727 | /* Magic code that will send notification when window is destroyed */ |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 2728 | xcb_intern_atom_cookie_t cookie = xcb_intern_atom(demo->connection, 1, 12, "WM_PROTOCOLS"); |
| 2729 | xcb_intern_atom_reply_t *reply = xcb_intern_atom_reply(demo->connection, cookie, 0); |
Courtney Goeltzenleuchter | 98cb2cb | 2014-11-06 14:27:52 -0700 | [diff] [blame] | 2730 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 2731 | xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW"); |
| 2732 | demo->atom_wm_delete_window = xcb_intern_atom_reply(demo->connection, cookie2, 0); |
Courtney Goeltzenleuchter | 98cb2cb | 2014-11-06 14:27:52 -0700 | [diff] [blame] | 2733 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 2734 | xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE, demo->xcb_window, (*reply).atom, 4, 32, 1, |
Courtney Goeltzenleuchter | 98cb2cb | 2014-11-06 14:27:52 -0700 | [diff] [blame] | 2735 | &(*demo->atom_wm_delete_window).atom); |
| 2736 | free(reply); |
| 2737 | |
Tony Barbour | 2593b18 | 2016-04-19 10:57:58 -0600 | [diff] [blame] | 2738 | xcb_map_window(demo->connection, demo->xcb_window); |
David Pinedo | 2bb7c93 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 2739 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2740 | // Force the x/y coordinates to 100,100 results are identical in consecutive |
| 2741 | // runs |
| 2742 | const uint32_t coords[] = {100, 100}; |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 2743 | xcb_configure_window(demo->connection, demo->xcb_window, XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, coords); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 2744 | } |
Tony Barbour | 6b13166 | 2016-08-25 13:14:45 -0600 | [diff] [blame] | 2745 | // VK_USE_PLATFORM_XCB_KHR |
| 2746 | #elif defined(VK_USE_PLATFORM_WAYLAND_KHR) |
Mun Gwan-gyeong | 6f01c55 | 2016-06-27 05:34:44 +0900 | [diff] [blame] | 2747 | static void demo_run(struct demo *demo) { |
| 2748 | while (!demo->quit) { |
Joey Bzdek | 15eb070 | 2017-06-07 09:40:36 -0600 | [diff] [blame] | 2749 | if (demo->pause) { |
| 2750 | wl_display_dispatch(demo->display); // block and wait for input |
Joey Bzdek | 33bc5c8 | 2017-06-14 10:33:36 -0600 | [diff] [blame] | 2751 | } else { |
Joey Bzdek | 15eb070 | 2017-06-07 09:40:36 -0600 | [diff] [blame] | 2752 | wl_display_dispatch_pending(demo->display); // don't block |
| 2753 | demo_draw(demo); |
| 2754 | demo->curFrame++; |
| 2755 | if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount) demo->quit = true; |
| 2756 | } |
Mun Gwan-gyeong | 6f01c55 | 2016-06-27 05:34:44 +0900 | [diff] [blame] | 2757 | } |
| 2758 | } |
| 2759 | |
Tony-LunarG | 5ceb7be | 2019-12-05 16:05:40 -0700 | [diff] [blame] | 2760 | static void handle_ping(void *data UNUSED, struct wl_shell_surface *shell_surface, uint32_t serial) { |
| 2761 | wl_shell_surface_pong(shell_surface, serial); |
Mun Gwan-gyeong | 6f01c55 | 2016-06-27 05:34:44 +0900 | [diff] [blame] | 2762 | } |
| 2763 | |
Tony-LunarG | 5ceb7be | 2019-12-05 16:05:40 -0700 | [diff] [blame] | 2764 | static void handle_configure(void *data UNUSED, struct wl_shell_surface *shell_surface UNUSED, uint32_t edges UNUSED, |
| 2765 | int32_t width UNUSED, int32_t height UNUSED) {} |
Mun Gwan-gyeong | 6f01c55 | 2016-06-27 05:34:44 +0900 | [diff] [blame] | 2766 | |
Tony-LunarG | 5ceb7be | 2019-12-05 16:05:40 -0700 | [diff] [blame] | 2767 | static void handle_popup_done(void *data UNUSED, struct wl_shell_surface *shell_surface UNUSED) {} |
Mun Gwan-gyeong | 6f01c55 | 2016-06-27 05:34:44 +0900 | [diff] [blame] | 2768 | |
Tony-LunarG | 5ceb7be | 2019-12-05 16:05:40 -0700 | [diff] [blame] | 2769 | static const struct wl_shell_surface_listener shell_surface_listener = {handle_ping, handle_configure, handle_popup_done}; |
Mun Gwan-gyeong | 6f01c55 | 2016-06-27 05:34:44 +0900 | [diff] [blame] | 2770 | |
| 2771 | static void demo_create_window(struct demo *demo) { |
| 2772 | demo->window = wl_compositor_create_surface(demo->compositor); |
| 2773 | if (!demo->window) { |
| 2774 | printf("Can not create wayland_surface from compositor!\n"); |
| 2775 | fflush(stdout); |
| 2776 | exit(1); |
| 2777 | } |
| 2778 | |
Tony-LunarG | 5ceb7be | 2019-12-05 16:05:40 -0700 | [diff] [blame] | 2779 | demo->shell_surface = wl_shell_get_shell_surface(demo->shell, demo->window); |
| 2780 | if (!demo->shell_surface) { |
| 2781 | printf("Can not get shell_surface from wayland_surface!\n"); |
Mun Gwan-gyeong | 6f01c55 | 2016-06-27 05:34:44 +0900 | [diff] [blame] | 2782 | fflush(stdout); |
| 2783 | exit(1); |
| 2784 | } |
Tony-LunarG | 5ceb7be | 2019-12-05 16:05:40 -0700 | [diff] [blame] | 2785 | wl_shell_surface_add_listener(demo->shell_surface, &shell_surface_listener, demo); |
| 2786 | wl_shell_surface_set_toplevel(demo->shell_surface); |
| 2787 | wl_shell_surface_set_title(demo->shell_surface, APP_SHORT_NAME); |
Mun Gwan-gyeong | 6f01c55 | 2016-06-27 05:34:44 +0900 | [diff] [blame] | 2788 | } |
Nicolas Caramelli | c8be8c8 | 2020-07-13 17:22:09 +0200 | [diff] [blame] | 2789 | #elif defined(VK_USE_PLATFORM_DIRECTFB_EXT) |
| 2790 | static void demo_create_directfb_window(struct demo *demo) { |
| 2791 | DFBResult ret; |
| 2792 | |
| 2793 | ret = DirectFBInit(NULL, NULL); |
| 2794 | if (ret) { |
| 2795 | printf("DirectFBInit failed to initialize DirectFB!\n"); |
| 2796 | fflush(stdout); |
| 2797 | exit(1); |
| 2798 | } |
| 2799 | |
| 2800 | ret = DirectFBCreate(&demo->dfb); |
| 2801 | if (ret) { |
| 2802 | printf("DirectFBCreate failed to create main interface of DirectFB!\n"); |
| 2803 | fflush(stdout); |
| 2804 | exit(1); |
| 2805 | } |
| 2806 | |
| 2807 | DFBSurfaceDescription desc; |
| 2808 | desc.flags = DSDESC_CAPS | DSDESC_WIDTH | DSDESC_HEIGHT; |
| 2809 | desc.caps = DSCAPS_PRIMARY; |
| 2810 | desc.width = demo->width; |
| 2811 | desc.height = demo->height; |
| 2812 | ret = demo->dfb->CreateSurface(demo->dfb, &desc, &demo->window); |
| 2813 | if (ret) { |
| 2814 | printf("CreateSurface failed to create DirectFB surface interface!\n"); |
| 2815 | fflush(stdout); |
| 2816 | exit(1); |
| 2817 | } |
| 2818 | |
| 2819 | ret = demo->dfb->CreateInputEventBuffer(demo->dfb, DICAPS_KEYS, DFB_FALSE, &demo->event_buffer); |
| 2820 | if (ret) { |
| 2821 | printf("CreateInputEventBuffer failed to create DirectFB event buffer interface!\n"); |
| 2822 | fflush(stdout); |
| 2823 | exit(1); |
| 2824 | } |
| 2825 | } |
| 2826 | |
| 2827 | static void demo_handle_directfb_event(struct demo *demo, const DFBInputEvent *event) { |
| 2828 | if (event->type != DIET_KEYPRESS) return; |
| 2829 | switch (event->key_symbol) { |
| 2830 | case DIKS_ESCAPE: // Escape |
| 2831 | demo->quit = true; |
| 2832 | break; |
| 2833 | case DIKS_CURSOR_LEFT: // left arrow key |
| 2834 | demo->spin_angle -= demo->spin_increment; |
| 2835 | break; |
| 2836 | case DIKS_CURSOR_RIGHT: // right arrow key |
| 2837 | demo->spin_angle += demo->spin_increment; |
| 2838 | break; |
| 2839 | case DIKS_SPACE: // space bar |
| 2840 | demo->pause = !demo->pause; |
| 2841 | break; |
| 2842 | default: |
| 2843 | break; |
| 2844 | } |
| 2845 | } |
| 2846 | |
| 2847 | static void demo_run_directfb(struct demo *demo) { |
| 2848 | while (!demo->quit) { |
| 2849 | DFBInputEvent event; |
| 2850 | |
| 2851 | if (demo->pause) { |
| 2852 | demo->event_buffer->WaitForEvent(demo->event_buffer); |
Nicolas Caramelli | 6d4f1c6 | 2020-07-13 18:10:07 +0200 | [diff] [blame] | 2853 | if (!demo->event_buffer->GetEvent(demo->event_buffer, DFB_EVENT(&event))) demo_handle_directfb_event(demo, &event); |
Nicolas Caramelli | c8be8c8 | 2020-07-13 17:22:09 +0200 | [diff] [blame] | 2854 | } else { |
Nicolas Caramelli | 6d4f1c6 | 2020-07-13 18:10:07 +0200 | [diff] [blame] | 2855 | if (!demo->event_buffer->GetEvent(demo->event_buffer, DFB_EVENT(&event))) demo_handle_directfb_event(demo, &event); |
Nicolas Caramelli | c8be8c8 | 2020-07-13 17:22:09 +0200 | [diff] [blame] | 2856 | |
| 2857 | demo_draw(demo); |
| 2858 | demo->curFrame++; |
| 2859 | if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount) demo->quit = true; |
| 2860 | } |
| 2861 | } |
| 2862 | } |
Michael Lentine | c6cde4b | 2016-04-18 13:20:45 -0500 | [diff] [blame] | 2863 | #elif defined(VK_USE_PLATFORM_ANDROID_KHR) |
| 2864 | static void demo_run(struct demo *demo) { |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 2865 | if (!demo->prepared) return; |
Michael Lentine | c6cde4b | 2016-04-18 13:20:45 -0500 | [diff] [blame] | 2866 | |
Michael Lentine | c6cde4b | 2016-04-18 13:20:45 -0500 | [diff] [blame] | 2867 | demo_draw(demo); |
Michael Lentine | c6cde4b | 2016-04-18 13:20:45 -0500 | [diff] [blame] | 2868 | demo->curFrame++; |
| 2869 | } |
Bill Hollings | 0a0625a | 2019-07-15 17:39:18 -0400 | [diff] [blame] | 2870 | #elif defined(VK_USE_PLATFORM_METAL_EXT) |
Karl Schultz | 206b1c5 | 2018-04-13 18:02:07 -0600 | [diff] [blame] | 2871 | static void demo_run(struct demo *demo) { |
| 2872 | demo_draw(demo); |
| 2873 | demo->curFrame++; |
| 2874 | if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount) { |
| 2875 | demo->quit = TRUE; |
| 2876 | } |
| 2877 | } |
Damien Leone | 600c305 | 2017-01-31 10:26:07 -0700 | [diff] [blame] | 2878 | #elif defined(VK_USE_PLATFORM_DISPLAY_KHR) |
| 2879 | static VkResult demo_create_display_surface(struct demo *demo) { |
| 2880 | VkResult U_ASSERT_ONLY err; |
| 2881 | uint32_t display_count; |
| 2882 | uint32_t mode_count; |
| 2883 | uint32_t plane_count; |
| 2884 | VkDisplayPropertiesKHR display_props; |
| 2885 | VkDisplayKHR display; |
| 2886 | VkDisplayModePropertiesKHR mode_props; |
| 2887 | VkDisplayPlanePropertiesKHR *plane_props; |
| 2888 | VkBool32 found_plane = VK_FALSE; |
| 2889 | uint32_t plane_index; |
| 2890 | VkExtent2D image_extent; |
| 2891 | VkDisplaySurfaceCreateInfoKHR create_info; |
| 2892 | |
| 2893 | // Get the first display |
Damien Leone | 600c305 | 2017-01-31 10:26:07 -0700 | [diff] [blame] | 2894 | display_count = 1; |
| 2895 | err = vkGetPhysicalDeviceDisplayPropertiesKHR(demo->gpu, &display_count, &display_props); |
| 2896 | assert(!err || (err == VK_INCOMPLETE)); |
| 2897 | |
| 2898 | display = display_props.display; |
| 2899 | |
| 2900 | // Get the first mode of the display |
| 2901 | err = vkGetDisplayModePropertiesKHR(demo->gpu, display, &mode_count, NULL); |
| 2902 | assert(!err); |
| 2903 | |
| 2904 | if (mode_count == 0) { |
| 2905 | printf("Cannot find any mode for the display!\n"); |
| 2906 | fflush(stdout); |
| 2907 | exit(1); |
| 2908 | } |
| 2909 | |
| 2910 | mode_count = 1; |
| 2911 | err = vkGetDisplayModePropertiesKHR(demo->gpu, display, &mode_count, &mode_props); |
| 2912 | assert(!err || (err == VK_INCOMPLETE)); |
| 2913 | |
| 2914 | // Get the list of planes |
| 2915 | err = vkGetPhysicalDeviceDisplayPlanePropertiesKHR(demo->gpu, &plane_count, NULL); |
| 2916 | assert(!err); |
| 2917 | |
| 2918 | if (plane_count == 0) { |
| 2919 | printf("Cannot find any plane!\n"); |
| 2920 | fflush(stdout); |
| 2921 | exit(1); |
| 2922 | } |
| 2923 | |
| 2924 | plane_props = malloc(sizeof(VkDisplayPlanePropertiesKHR) * plane_count); |
| 2925 | assert(plane_props); |
| 2926 | |
| 2927 | err = vkGetPhysicalDeviceDisplayPlanePropertiesKHR(demo->gpu, &plane_count, plane_props); |
| 2928 | assert(!err); |
| 2929 | |
| 2930 | // Find a plane compatible with the display |
| 2931 | for (plane_index = 0; plane_index < plane_count; plane_index++) { |
| 2932 | uint32_t supported_count; |
| 2933 | VkDisplayKHR *supported_displays; |
| 2934 | |
| 2935 | // Disqualify planes that are bound to a different display |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 2936 | if ((plane_props[plane_index].currentDisplay != VK_NULL_HANDLE) && (plane_props[plane_index].currentDisplay != display)) { |
Damien Leone | 600c305 | 2017-01-31 10:26:07 -0700 | [diff] [blame] | 2937 | continue; |
| 2938 | } |
| 2939 | |
| 2940 | err = vkGetDisplayPlaneSupportedDisplaysKHR(demo->gpu, plane_index, &supported_count, NULL); |
| 2941 | assert(!err); |
| 2942 | |
| 2943 | if (supported_count == 0) { |
| 2944 | continue; |
| 2945 | } |
| 2946 | |
| 2947 | supported_displays = malloc(sizeof(VkDisplayKHR) * supported_count); |
| 2948 | assert(supported_displays); |
| 2949 | |
| 2950 | err = vkGetDisplayPlaneSupportedDisplaysKHR(demo->gpu, plane_index, &supported_count, supported_displays); |
| 2951 | assert(!err); |
| 2952 | |
| 2953 | for (uint32_t i = 0; i < supported_count; i++) { |
| 2954 | if (supported_displays[i] == display) { |
| 2955 | found_plane = VK_TRUE; |
| 2956 | break; |
| 2957 | } |
| 2958 | } |
| 2959 | |
| 2960 | free(supported_displays); |
| 2961 | |
| 2962 | if (found_plane) { |
| 2963 | break; |
| 2964 | } |
| 2965 | } |
| 2966 | |
| 2967 | if (!found_plane) { |
| 2968 | printf("Cannot find a plane compatible with the display!\n"); |
| 2969 | fflush(stdout); |
| 2970 | exit(1); |
| 2971 | } |
| 2972 | |
| 2973 | free(plane_props); |
| 2974 | |
Tony Barbour | 820b55b | 2017-03-14 08:55:46 -0600 | [diff] [blame] | 2975 | VkDisplayPlaneCapabilitiesKHR planeCaps; |
| 2976 | vkGetDisplayPlaneCapabilitiesKHR(demo->gpu, mode_props.displayMode, plane_index, &planeCaps); |
| 2977 | // Find a supported alpha mode |
Mark Young | 66510e5 | 2017-11-10 10:05:14 -0700 | [diff] [blame] | 2978 | VkCompositeAlphaFlagBitsKHR alphaMode = VK_DISPLAY_PLANE_ALPHA_OPAQUE_BIT_KHR; |
| 2979 | VkCompositeAlphaFlagBitsKHR alphaModes[4] = { |
Tony Barbour | 820b55b | 2017-03-14 08:55:46 -0600 | [diff] [blame] | 2980 | VK_DISPLAY_PLANE_ALPHA_OPAQUE_BIT_KHR, |
| 2981 | VK_DISPLAY_PLANE_ALPHA_GLOBAL_BIT_KHR, |
| 2982 | VK_DISPLAY_PLANE_ALPHA_PER_PIXEL_BIT_KHR, |
| 2983 | VK_DISPLAY_PLANE_ALPHA_PER_PIXEL_PREMULTIPLIED_BIT_KHR, |
| 2984 | }; |
| 2985 | for (uint32_t i = 0; i < sizeof(alphaModes); i++) { |
| 2986 | if (planeCaps.supportedAlpha & alphaModes[i]) { |
| 2987 | alphaMode = alphaModes[i]; |
| 2988 | break; |
| 2989 | } |
| 2990 | } |
Damien Leone | 600c305 | 2017-01-31 10:26:07 -0700 | [diff] [blame] | 2991 | image_extent.width = mode_props.parameters.visibleRegion.width; |
| 2992 | image_extent.height = mode_props.parameters.visibleRegion.height; |
| 2993 | |
| 2994 | create_info.sType = VK_STRUCTURE_TYPE_DISPLAY_SURFACE_CREATE_INFO_KHR; |
| 2995 | create_info.pNext = NULL; |
| 2996 | create_info.flags = 0; |
| 2997 | create_info.displayMode = mode_props.displayMode; |
| 2998 | create_info.planeIndex = plane_index; |
| 2999 | create_info.planeStackIndex = plane_props[plane_index].currentStackIndex; |
| 3000 | create_info.transform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR; |
Tony Barbour | 820b55b | 2017-03-14 08:55:46 -0600 | [diff] [blame] | 3001 | create_info.alphaMode = alphaMode; |
Damien Leone | 600c305 | 2017-01-31 10:26:07 -0700 | [diff] [blame] | 3002 | create_info.globalAlpha = 1.0f; |
| 3003 | create_info.imageExtent = image_extent; |
| 3004 | |
| 3005 | return vkCreateDisplayPlaneSurfaceKHR(demo->inst, &create_info, NULL, &demo->surface); |
| 3006 | } |
| 3007 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 3008 | static void demo_run_display(struct demo *demo) { |
Damien Leone | 600c305 | 2017-01-31 10:26:07 -0700 | [diff] [blame] | 3009 | while (!demo->quit) { |
| 3010 | demo_draw(demo); |
| 3011 | demo->curFrame++; |
| 3012 | |
| 3013 | if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount) { |
| 3014 | demo->quit = true; |
| 3015 | } |
| 3016 | } |
| 3017 | } |
Michael Lentine | c6cde4b | 2016-04-18 13:20:45 -0500 | [diff] [blame] | 3018 | #endif |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 3019 | |
Courtney Goeltzenleuchter | 886f76c | 2015-07-06 17:46:11 -0600 | [diff] [blame] | 3020 | /* |
| 3021 | * Return 1 (true) if all layer names specified in check_names |
| 3022 | * can be found in given layer properties. |
| 3023 | */ |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 3024 | static VkBool32 demo_check_layers(uint32_t check_count, char **check_names, uint32_t layer_count, VkLayerProperties *layers) { |
Courtney Goeltzenleuchter | 886f76c | 2015-07-06 17:46:11 -0600 | [diff] [blame] | 3025 | for (uint32_t i = 0; i < check_count; i++) { |
Courtney Goeltzenleuchter | c88ce51 | 2015-07-09 11:44:38 -0600 | [diff] [blame] | 3026 | VkBool32 found = 0; |
Courtney Goeltzenleuchter | 886f76c | 2015-07-06 17:46:11 -0600 | [diff] [blame] | 3027 | for (uint32_t j = 0; j < layer_count; j++) { |
| 3028 | if (!strcmp(check_names[i], layers[j].layerName)) { |
| 3029 | found = 1; |
Dustin Graves | 7c28735 | 2016-01-27 13:48:06 -0700 | [diff] [blame] | 3030 | break; |
Courtney Goeltzenleuchter | 886f76c | 2015-07-06 17:46:11 -0600 | [diff] [blame] | 3031 | } |
| 3032 | } |
| 3033 | if (!found) { |
| 3034 | fprintf(stderr, "Cannot find layer: %s\n", check_names[i]); |
| 3035 | return 0; |
| 3036 | } |
| 3037 | } |
| 3038 | return 1; |
| 3039 | } |
Tony-LunarG | 27c2124 | 2021-03-11 16:28:19 -0700 | [diff] [blame] | 3040 | #if defined(VK_USE_PLATFORM_DISPLAY_KHR) |
| 3041 | int find_display_gpu(int gpu_number, uint32_t gpu_count, VkPhysicalDevice *physical_devices) { |
| 3042 | uint32_t display_count = 0; |
| 3043 | VkResult result; |
| 3044 | int gpu_return = gpu_number; |
| 3045 | if (gpu_number >= 0) { |
| 3046 | result = vkGetPhysicalDeviceDisplayPropertiesKHR(physical_devices[gpu_number], &display_count, NULL); |
| 3047 | assert(!result); |
| 3048 | } else { |
| 3049 | for (uint32_t i = 0; i < gpu_count; i++) { |
| 3050 | result = vkGetPhysicalDeviceDisplayPropertiesKHR(physical_devices[i], &display_count, NULL); |
| 3051 | assert(!result); |
| 3052 | if (display_count) { |
| 3053 | gpu_return = i; |
| 3054 | break; |
| 3055 | } |
| 3056 | } |
| 3057 | } |
| 3058 | if (display_count > 0) |
| 3059 | return gpu_return; |
| 3060 | else |
| 3061 | return -1; |
| 3062 | } |
| 3063 | #endif |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 3064 | static void demo_init_vk(struct demo *demo) { |
Tobin Ehlis | 99239dc | 2015-04-16 18:04:57 -0600 | [diff] [blame] | 3065 | VkResult err; |
Courtney Goeltzenleuchter | 922a0fa | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 3066 | uint32_t instance_extension_count = 0; |
Courtney Goeltzenleuchter | be10336 | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 3067 | uint32_t instance_layer_count = 0; |
Mark Lobodzinski | 96f2ff3 | 2019-03-27 12:37:30 -0600 | [diff] [blame] | 3068 | char *instance_validation_layers[] = {"VK_LAYER_KHRONOS_validation"}; |
Karl Schultz | 7c50ad6 | 2016-04-01 12:07:03 -0600 | [diff] [blame] | 3069 | demo->enabled_extension_count = 0; |
| 3070 | demo->enabled_layer_count = 0; |
Jeremy Kniager | 602e418 | 2018-01-19 10:52:34 -0700 | [diff] [blame] | 3071 | demo->is_minimized = false; |
| 3072 | demo->cmd_pool = VK_NULL_HANDLE; |
Courtney Goeltzenleuchter | be10336 | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 3073 | |
Mark Lobodzinski | 96f2ff3 | 2019-03-27 12:37:30 -0600 | [diff] [blame] | 3074 | // Look for validation layers |
Courtney Goeltzenleuchter | c88ce51 | 2015-07-09 11:44:38 -0600 | [diff] [blame] | 3075 | VkBool32 validation_found = 0; |
Karl Schultz | 7c50ad6 | 2016-04-01 12:07:03 -0600 | [diff] [blame] | 3076 | if (demo->validate) { |
Karl Schultz | 7c50ad6 | 2016-04-01 12:07:03 -0600 | [diff] [blame] | 3077 | err = vkEnumerateInstanceLayerProperties(&instance_layer_count, NULL); |
Dustin Graves | 7c28735 | 2016-01-27 13:48:06 -0700 | [diff] [blame] | 3078 | assert(!err); |
Courtney Goeltzenleuchter | 886f76c | 2015-07-06 17:46:11 -0600 | [diff] [blame] | 3079 | |
Karl Schultz | 7c50ad6 | 2016-04-01 12:07:03 -0600 | [diff] [blame] | 3080 | if (instance_layer_count > 0) { |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 3081 | VkLayerProperties *instance_layers = malloc(sizeof(VkLayerProperties) * instance_layer_count); |
| 3082 | err = vkEnumerateInstanceLayerProperties(&instance_layer_count, instance_layers); |
Karl Schultz | 7c50ad6 | 2016-04-01 12:07:03 -0600 | [diff] [blame] | 3083 | assert(!err); |
Dustin Graves | 7c28735 | 2016-01-27 13:48:06 -0700 | [diff] [blame] | 3084 | |
Mark Lobodzinski | 96f2ff3 | 2019-03-27 12:37:30 -0600 | [diff] [blame] | 3085 | validation_found = demo_check_layers(ARRAY_SIZE(instance_validation_layers), instance_validation_layers, |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 3086 | instance_layer_count, instance_layers); |
Karl Schultz | 7c50ad6 | 2016-04-01 12:07:03 -0600 | [diff] [blame] | 3087 | if (validation_found) { |
Mark Lobodzinski | 96f2ff3 | 2019-03-27 12:37:30 -0600 | [diff] [blame] | 3088 | demo->enabled_layer_count = ARRAY_SIZE(instance_validation_layers); |
| 3089 | demo->enabled_layers[0] = "VK_LAYER_KHRONOS_validation"; |
Karl Schultz | 7c50ad6 | 2016-04-01 12:07:03 -0600 | [diff] [blame] | 3090 | } |
| 3091 | free(instance_layers); |
Courtney Goeltzenleuchter | be10336 | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 3092 | } |
Dustin Graves | 7c28735 | 2016-01-27 13:48:06 -0700 | [diff] [blame] | 3093 | |
Karl Schultz | 7c50ad6 | 2016-04-01 12:07:03 -0600 | [diff] [blame] | 3094 | if (!validation_found) { |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 3095 | ERR_EXIT( |
| 3096 | "vkEnumerateInstanceLayerProperties failed to find required validation layer.\n\n" |
| 3097 | "Please look at the Getting Started guide for additional information.\n", |
| 3098 | "vkCreateInstance Failure"); |
Karl Schultz | 7c50ad6 | 2016-04-01 12:07:03 -0600 | [diff] [blame] | 3099 | } |
Dustin Graves | 7c28735 | 2016-01-27 13:48:06 -0700 | [diff] [blame] | 3100 | } |
| 3101 | |
| 3102 | /* Look for instance extensions */ |
| 3103 | VkBool32 surfaceExtFound = 0; |
| 3104 | VkBool32 platformSurfaceExtFound = 0; |
Karl Schultz | 7c50ad6 | 2016-04-01 12:07:03 -0600 | [diff] [blame] | 3105 | memset(demo->extension_names, 0, sizeof(demo->extension_names)); |
Dustin Graves | 7c28735 | 2016-01-27 13:48:06 -0700 | [diff] [blame] | 3106 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 3107 | err = vkEnumerateInstanceExtensionProperties(NULL, &instance_extension_count, NULL); |
Courtney Goeltzenleuchter | be10336 | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 3108 | assert(!err); |
| 3109 | |
Dustin Graves | 7c28735 | 2016-01-27 13:48:06 -0700 | [diff] [blame] | 3110 | if (instance_extension_count > 0) { |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 3111 | VkExtensionProperties *instance_extensions = malloc(sizeof(VkExtensionProperties) * instance_extension_count); |
| 3112 | err = vkEnumerateInstanceExtensionProperties(NULL, &instance_extension_count, instance_extensions); |
Dustin Graves | 7c28735 | 2016-01-27 13:48:06 -0700 | [diff] [blame] | 3113 | assert(!err); |
| 3114 | for (uint32_t i = 0; i < instance_extension_count; i++) { |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 3115 | if (!strcmp(VK_KHR_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) { |
Dustin Graves | 7c28735 | 2016-01-27 13:48:06 -0700 | [diff] [blame] | 3116 | surfaceExtFound = 1; |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 3117 | demo->extension_names[demo->enabled_extension_count++] = VK_KHR_SURFACE_EXTENSION_NAME; |
Courtney Goeltzenleuchter | be10336 | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 3118 | } |
Michael Lentine | c6cde4b | 2016-04-18 13:20:45 -0500 | [diff] [blame] | 3119 | #if defined(VK_USE_PLATFORM_WIN32_KHR) |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 3120 | if (!strcmp(VK_KHR_WIN32_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) { |
Dustin Graves | 7c28735 | 2016-01-27 13:48:06 -0700 | [diff] [blame] | 3121 | platformSurfaceExtFound = 1; |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 3122 | demo->extension_names[demo->enabled_extension_count++] = VK_KHR_WIN32_SURFACE_EXTENSION_NAME; |
Dustin Graves | 7c28735 | 2016-01-27 13:48:06 -0700 | [diff] [blame] | 3123 | } |
Tony Barbour | 153cb06 | 2016-12-07 13:43:36 -0700 | [diff] [blame] | 3124 | #elif defined(VK_USE_PLATFORM_XLIB_KHR) |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 3125 | if (!strcmp(VK_KHR_XLIB_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) { |
Tony Barbour | 2593b18 | 2016-04-19 10:57:58 -0600 | [diff] [blame] | 3126 | platformSurfaceExtFound = 1; |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 3127 | demo->extension_names[demo->enabled_extension_count++] = VK_KHR_XLIB_SURFACE_EXTENSION_NAME; |
Tony Barbour | 2593b18 | 2016-04-19 10:57:58 -0600 | [diff] [blame] | 3128 | } |
Tony Barbour | 153cb06 | 2016-12-07 13:43:36 -0700 | [diff] [blame] | 3129 | #elif defined(VK_USE_PLATFORM_XCB_KHR) |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 3130 | if (!strcmp(VK_KHR_XCB_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) { |
Dustin Graves | 7c28735 | 2016-01-27 13:48:06 -0700 | [diff] [blame] | 3131 | platformSurfaceExtFound = 1; |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 3132 | demo->extension_names[demo->enabled_extension_count++] = VK_KHR_XCB_SURFACE_EXTENSION_NAME; |
Dustin Graves | 7c28735 | 2016-01-27 13:48:06 -0700 | [diff] [blame] | 3133 | } |
Tony Barbour | 153cb06 | 2016-12-07 13:43:36 -0700 | [diff] [blame] | 3134 | #elif defined(VK_USE_PLATFORM_WAYLAND_KHR) |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 3135 | if (!strcmp(VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) { |
Mun Gwan-gyeong | 6f01c55 | 2016-06-27 05:34:44 +0900 | [diff] [blame] | 3136 | platformSurfaceExtFound = 1; |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 3137 | demo->extension_names[demo->enabled_extension_count++] = VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME; |
Mun Gwan-gyeong | 6f01c55 | 2016-06-27 05:34:44 +0900 | [diff] [blame] | 3138 | } |
Nicolas Caramelli | c8be8c8 | 2020-07-13 17:22:09 +0200 | [diff] [blame] | 3139 | #elif defined(VK_USE_PLATFORM_DIRECTFB_EXT) |
| 3140 | if (!strcmp(VK_EXT_DIRECTFB_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) { |
| 3141 | platformSurfaceExtFound = 1; |
| 3142 | demo->extension_names[demo->enabled_extension_count++] = VK_EXT_DIRECTFB_SURFACE_EXTENSION_NAME; |
| 3143 | } |
Damien Leone | 600c305 | 2017-01-31 10:26:07 -0700 | [diff] [blame] | 3144 | #elif defined(VK_USE_PLATFORM_DISPLAY_KHR) |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 3145 | if (!strcmp(VK_KHR_DISPLAY_EXTENSION_NAME, instance_extensions[i].extensionName)) { |
Damien Leone | 600c305 | 2017-01-31 10:26:07 -0700 | [diff] [blame] | 3146 | platformSurfaceExtFound = 1; |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 3147 | demo->extension_names[demo->enabled_extension_count++] = VK_KHR_DISPLAY_EXTENSION_NAME; |
Damien Leone | 600c305 | 2017-01-31 10:26:07 -0700 | [diff] [blame] | 3148 | } |
Tony Barbour | 153cb06 | 2016-12-07 13:43:36 -0700 | [diff] [blame] | 3149 | #elif defined(VK_USE_PLATFORM_ANDROID_KHR) |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 3150 | if (!strcmp(VK_KHR_ANDROID_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) { |
Michael Lentine | c6cde4b | 2016-04-18 13:20:45 -0500 | [diff] [blame] | 3151 | platformSurfaceExtFound = 1; |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 3152 | demo->extension_names[demo->enabled_extension_count++] = VK_KHR_ANDROID_SURFACE_EXTENSION_NAME; |
Michael Lentine | c6cde4b | 2016-04-18 13:20:45 -0500 | [diff] [blame] | 3153 | } |
Bill Hollings | 0a0625a | 2019-07-15 17:39:18 -0400 | [diff] [blame] | 3154 | #elif defined(VK_USE_PLATFORM_METAL_EXT) |
| 3155 | if (!strcmp(VK_EXT_METAL_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) { |
Bill Hollings | f435214 | 2017-02-14 22:58:56 -0500 | [diff] [blame] | 3156 | platformSurfaceExtFound = 1; |
Bill Hollings | 0a0625a | 2019-07-15 17:39:18 -0400 | [diff] [blame] | 3157 | demo->extension_names[demo->enabled_extension_count++] = VK_EXT_METAL_SURFACE_EXTENSION_NAME; |
Bill Hollings | f435214 | 2017-02-14 22:58:56 -0500 | [diff] [blame] | 3158 | } |
Michael Lentine | c6cde4b | 2016-04-18 13:20:45 -0500 | [diff] [blame] | 3159 | #endif |
Richard S. Wright Jr | ca51bc7 | 2021-01-06 13:03:18 -0500 | [diff] [blame] | 3160 | if (!strcmp(VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME, instance_extensions[i].extensionName)) { |
| 3161 | demo->extension_names[demo->enabled_extension_count++] = VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME; |
| 3162 | } |
Mark Young | 66510e5 | 2017-11-10 10:05:14 -0700 | [diff] [blame] | 3163 | if (!strcmp(VK_EXT_DEBUG_UTILS_EXTENSION_NAME, instance_extensions[i].extensionName)) { |
| 3164 | if (demo->validate) { |
| 3165 | demo->extension_names[demo->enabled_extension_count++] = VK_EXT_DEBUG_UTILS_EXTENSION_NAME; |
| 3166 | } |
| 3167 | } |
Karl Schultz | 7c50ad6 | 2016-04-01 12:07:03 -0600 | [diff] [blame] | 3168 | assert(demo->enabled_extension_count < 64); |
Courtney Goeltzenleuchter | 922a0fa | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 3169 | } |
Dustin Graves | 7c28735 | 2016-01-27 13:48:06 -0700 | [diff] [blame] | 3170 | |
| 3171 | free(instance_extensions); |
Tobin Ehlis | 99239dc | 2015-04-16 18:04:57 -0600 | [diff] [blame] | 3172 | } |
Dustin Graves | 7c28735 | 2016-01-27 13:48:06 -0700 | [diff] [blame] | 3173 | |
Ian Elliott | d3f7dcc | 2015-11-20 13:08:34 -0700 | [diff] [blame] | 3174 | if (!surfaceExtFound) { |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 3175 | ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_KHR_SURFACE_EXTENSION_NAME |
| 3176 | " extension.\n\n" |
| 3177 | "Do you have a compatible Vulkan installable client driver (ICD) installed?\n" |
| 3178 | "Please look at the Getting Started guide for additional information.\n", |
Ian Elliott | 6515291 | 2015-04-28 13:22:33 -0600 | [diff] [blame] | 3179 | "vkCreateInstance Failure"); |
| 3180 | } |
Ian Elliott | d3f7dcc | 2015-11-20 13:08:34 -0700 | [diff] [blame] | 3181 | if (!platformSurfaceExtFound) { |
Michael Lentine | c6cde4b | 2016-04-18 13:20:45 -0500 | [diff] [blame] | 3182 | #if defined(VK_USE_PLATFORM_WIN32_KHR) |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 3183 | ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_KHR_WIN32_SURFACE_EXTENSION_NAME |
| 3184 | " extension.\n\n" |
| 3185 | "Do you have a compatible Vulkan installable client driver (ICD) installed?\n" |
| 3186 | "Please look at the Getting Started guide for additional information.\n", |
Ian Elliott | d3f7dcc | 2015-11-20 13:08:34 -0700 | [diff] [blame] | 3187 | "vkCreateInstance Failure"); |
Bill Hollings | 0a0625a | 2019-07-15 17:39:18 -0400 | [diff] [blame] | 3188 | #elif defined(VK_USE_PLATFORM_METAL_EXT) |
| 3189 | ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_EXT_METAL_SURFACE_EXTENSION_NAME |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 3190 | " extension.\n\n" |
| 3191 | "Do you have a compatible Vulkan installable client driver (ICD) installed?\n" |
| 3192 | "Please look at the Getting Started guide for additional information.\n", |
Tony Barbour | c65cd75 | 2017-03-13 15:29:35 -0600 | [diff] [blame] | 3193 | "vkCreateInstance Failure"); |
Michael Lentine | c6cde4b | 2016-04-18 13:20:45 -0500 | [diff] [blame] | 3194 | #elif defined(VK_USE_PLATFORM_XCB_KHR) |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 3195 | ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_KHR_XCB_SURFACE_EXTENSION_NAME |
| 3196 | " extension.\n\n" |
| 3197 | "Do you have a compatible Vulkan installable client driver (ICD) installed?\n" |
| 3198 | "Please look at the Getting Started guide for additional information.\n", |
Mark Lobodzinski | 6f7a603 | 2015-11-24 12:04:15 -0700 | [diff] [blame] | 3199 | "vkCreateInstance Failure"); |
Mun Gwan-gyeong | 6f01c55 | 2016-06-27 05:34:44 +0900 | [diff] [blame] | 3200 | #elif defined(VK_USE_PLATFORM_WAYLAND_KHR) |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 3201 | ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME |
| 3202 | " extension.\n\n" |
| 3203 | "Do you have a compatible Vulkan installable client driver (ICD) installed?\n" |
| 3204 | "Please look at the Getting Started guide for additional information.\n", |
Mun Gwan-gyeong | 6f01c55 | 2016-06-27 05:34:44 +0900 | [diff] [blame] | 3205 | "vkCreateInstance Failure"); |
Damien Leone | 600c305 | 2017-01-31 10:26:07 -0700 | [diff] [blame] | 3206 | #elif defined(VK_USE_PLATFORM_DISPLAY_KHR) |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 3207 | ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_KHR_DISPLAY_EXTENSION_NAME |
| 3208 | " extension.\n\n" |
| 3209 | "Do you have a compatible Vulkan installable client driver (ICD) installed?\n" |
| 3210 | "Please look at the Getting Started guide for additional information.\n", |
Damien Leone | 600c305 | 2017-01-31 10:26:07 -0700 | [diff] [blame] | 3211 | "vkCreateInstance Failure"); |
Michael Lentine | c6cde4b | 2016-04-18 13:20:45 -0500 | [diff] [blame] | 3212 | #elif defined(VK_USE_PLATFORM_ANDROID_KHR) |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 3213 | ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_KHR_ANDROID_SURFACE_EXTENSION_NAME |
| 3214 | " extension.\n\n" |
| 3215 | "Do you have a compatible Vulkan installable client driver (ICD) installed?\n" |
| 3216 | "Please look at the Getting Started guide for additional information.\n", |
Michael Lentine | c6cde4b | 2016-04-18 13:20:45 -0500 | [diff] [blame] | 3217 | "vkCreateInstance Failure"); |
Tony Barbour | 153cb06 | 2016-12-07 13:43:36 -0700 | [diff] [blame] | 3218 | #elif defined(VK_USE_PLATFORM_XLIB_KHR) |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 3219 | ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_KHR_XLIB_SURFACE_EXTENSION_NAME |
| 3220 | " extension.\n\n" |
| 3221 | "Do you have a compatible Vulkan installable client driver (ICD) installed?\n" |
| 3222 | "Please look at the Getting Started guide for additional information.\n", |
Tony Barbour | 2593b18 | 2016-04-19 10:57:58 -0600 | [diff] [blame] | 3223 | "vkCreateInstance Failure"); |
Nicolas Caramelli | c8be8c8 | 2020-07-13 17:22:09 +0200 | [diff] [blame] | 3224 | #elif defined(VK_USE_PLATFORM_DIRECTFB_EXT) |
| 3225 | ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_EXT_DIRECTFB_SURFACE_EXTENSION_NAME |
| 3226 | " extension.\n\n" |
| 3227 | "Do you have a compatible Vulkan installable client driver (ICD) installed?\n" |
| 3228 | "Please look at the Getting Started guide for additional information.\n", |
| 3229 | "vkCreateInstance Failure"); |
Michael Lentine | c6cde4b | 2016-04-18 13:20:45 -0500 | [diff] [blame] | 3230 | #endif |
Tony Barbour | 153cb06 | 2016-12-07 13:43:36 -0700 | [diff] [blame] | 3231 | } |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 3232 | const VkApplicationInfo app = { |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 3233 | .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 3234 | .pNext = NULL, |
Chia-I Wu | a8fe78a | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 3235 | .pApplicationName = APP_SHORT_NAME, |
| 3236 | .applicationVersion = 0, |
Ian Elliott | 44e33f7 | 2015-04-28 10:52:52 -0600 | [diff] [blame] | 3237 | .pEngineName = APP_SHORT_NAME, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 3238 | .engineVersion = 0, |
Jon Ashburn | 7f4bc2c | 2016-03-22 13:57:46 -0600 | [diff] [blame] | 3239 | .apiVersion = VK_API_VERSION_1_0, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 3240 | }; |
Tony Barbour | 3dddd5d | 2015-04-29 16:19:20 -0600 | [diff] [blame] | 3241 | VkInstanceCreateInfo inst_info = { |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 3242 | .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO, |
Jon Ashburn | ab46b36 | 2015-04-04 14:52:07 -0600 | [diff] [blame] | 3243 | .pNext = NULL, |
Chia-I Wu | a8fe78a | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 3244 | .pApplicationInfo = &app, |
Karl Schultz | 7c50ad6 | 2016-04-01 12:07:03 -0600 | [diff] [blame] | 3245 | .enabledLayerCount = demo->enabled_layer_count, |
| 3246 | .ppEnabledLayerNames = (const char *const *)instance_validation_layers, |
| 3247 | .enabledExtensionCount = demo->enabled_extension_count, |
| 3248 | .ppEnabledExtensionNames = (const char *const *)demo->extension_names, |
Jon Ashburn | ab46b36 | 2015-04-04 14:52:07 -0600 | [diff] [blame] | 3249 | }; |
Karl Schultz | cd9887d | 2016-03-25 14:25:16 -0600 | [diff] [blame] | 3250 | |
| 3251 | /* |
| 3252 | * This is info for a temp callback to use during CreateInstance. |
| 3253 | * After the instance is created, we use the instance-based |
| 3254 | * function to register the final callback. |
| 3255 | */ |
Mark Young | 66510e5 | 2017-11-10 10:05:14 -0700 | [diff] [blame] | 3256 | VkDebugUtilsMessengerCreateInfoEXT dbg_messenger_create_info; |
Ian Elliott | 5959bbd | 2016-03-25 09:07:19 -0600 | [diff] [blame] | 3257 | if (demo->validate) { |
Mark Young | 66510e5 | 2017-11-10 10:05:14 -0700 | [diff] [blame] | 3258 | // VK_EXT_debug_utils style |
| 3259 | dbg_messenger_create_info.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT; |
| 3260 | dbg_messenger_create_info.pNext = NULL; |
| 3261 | dbg_messenger_create_info.flags = 0; |
| 3262 | dbg_messenger_create_info.messageSeverity = |
| 3263 | VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT; |
| 3264 | dbg_messenger_create_info.messageType = VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT | |
| 3265 | VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT | |
| 3266 | VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT; |
| 3267 | dbg_messenger_create_info.pfnUserCallback = debug_messenger_callback; |
| 3268 | dbg_messenger_create_info.pUserData = demo; |
| 3269 | inst_info.pNext = &dbg_messenger_create_info; |
Ian Elliott | 5959bbd | 2016-03-25 09:07:19 -0600 | [diff] [blame] | 3270 | } |
Ian Elliott | 097d9f3 | 2015-04-28 11:35:02 -0600 | [diff] [blame] | 3271 | |
Chia-I Wu | 6363606 | 2015-10-26 21:10:41 +0800 | [diff] [blame] | 3272 | err = vkCreateInstance(&inst_info, NULL, &demo->inst); |
Ian Elliott | 0726413 | 2015-04-28 11:35:02 -0600 | [diff] [blame] | 3273 | if (err == VK_ERROR_INCOMPATIBLE_DRIVER) { |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 3274 | ERR_EXIT( |
| 3275 | "Cannot find a compatible Vulkan installable client driver (ICD).\n\n" |
| 3276 | "Please look at the Getting Started guide for additional information.\n", |
| 3277 | "vkCreateInstance Failure"); |
Courtney Goeltzenleuchter | 1e47e4b | 2015-09-14 18:01:17 -0600 | [diff] [blame] | 3278 | } else if (err == VK_ERROR_EXTENSION_NOT_PRESENT) { |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 3279 | ERR_EXIT( |
| 3280 | "Cannot find a specified extension library.\n" |
| 3281 | "Make sure your layers path is set appropriately.\n", |
| 3282 | "vkCreateInstance Failure"); |
Ian Elliott | 0726413 | 2015-04-28 11:35:02 -0600 | [diff] [blame] | 3283 | } else if (err) { |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 3284 | ERR_EXIT( |
| 3285 | "vkCreateInstance failed.\n\n" |
| 3286 | "Do you have a compatible Vulkan installable client driver (ICD) installed?\n" |
| 3287 | "Please look at the Getting Started guide for additional information.\n", |
| 3288 | "vkCreateInstance Failure"); |
Ian Elliott | 3979e28 | 2015-04-03 15:24:55 -0600 | [diff] [blame] | 3289 | } |
Jon Ashburn | ab46b36 | 2015-04-04 14:52:07 -0600 | [diff] [blame] | 3290 | |
Witold Baryluk | a3b988f | 2021-01-07 19:03:02 +0000 | [diff] [blame] | 3291 | /* Make initial call to query gpu_count, then second call for gpu info */ |
| 3292 | uint32_t gpu_count = 0; |
Tobin Ehlis | 8a724d8 | 2015-09-07 15:16:39 -0600 | [diff] [blame] | 3293 | err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, NULL); |
Petr Kraus | 2f1dbdb | 2018-04-14 14:45:17 +0200 | [diff] [blame] | 3294 | assert(!err); |
Dustin Graves | 7c28735 | 2016-01-27 13:48:06 -0700 | [diff] [blame] | 3295 | |
Witold Baryluk | a3b988f | 2021-01-07 19:03:02 +0000 | [diff] [blame] | 3296 | if (gpu_count <= 0) { |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 3297 | ERR_EXIT( |
| 3298 | "vkEnumeratePhysicalDevices reported zero accessible devices.\n\n" |
| 3299 | "Do you have a compatible Vulkan installable client driver (ICD) installed?\n" |
| 3300 | "Please look at the Getting Started guide for additional information.\n", |
| 3301 | "vkEnumeratePhysicalDevices Failure"); |
Dustin Graves | 7c28735 | 2016-01-27 13:48:06 -0700 | [diff] [blame] | 3302 | } |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 3303 | |
Witold Baryluk | a3b988f | 2021-01-07 19:03:02 +0000 | [diff] [blame] | 3304 | VkPhysicalDevice *physical_devices = malloc(sizeof(VkPhysicalDevice) * gpu_count); |
| 3305 | err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, physical_devices); |
| 3306 | assert(!err); |
| 3307 | if (demo->gpu_number >= 0 && !((uint32_t)demo->gpu_number < gpu_count)) { |
| 3308 | fprintf(stderr, "GPU %d specified is not present, GPU count = %u\n", demo->gpu_number, gpu_count); |
| 3309 | ERR_EXIT("Specified GPU number is not present", "User Error"); |
| 3310 | } |
| 3311 | |
Tony-LunarG | 27c2124 | 2021-03-11 16:28:19 -0700 | [diff] [blame] | 3312 | #if defined(VK_USE_PLATFORM_DISPLAY_KHR) |
| 3313 | demo->gpu_number = find_display_gpu(demo->gpu_number, gpu_count, physical_devices); |
| 3314 | if (demo->gpu_number < 0) { |
| 3315 | printf("Cannot find any display!\n"); |
| 3316 | fflush(stdout); |
| 3317 | exit(1); |
| 3318 | } |
| 3319 | #else |
Witold Baryluk | a3b988f | 2021-01-07 19:03:02 +0000 | [diff] [blame] | 3320 | /* Try to auto select most suitable device */ |
| 3321 | if (demo->gpu_number == -1) { |
| 3322 | uint32_t count_device_type[VK_PHYSICAL_DEVICE_TYPE_CPU + 1]; |
| 3323 | memset(count_device_type, 0, sizeof(count_device_type)); |
| 3324 | |
| 3325 | VkPhysicalDeviceProperties physicalDeviceProperties; |
| 3326 | for (uint32_t i = 0; i < gpu_count; i++) { |
| 3327 | vkGetPhysicalDeviceProperties(physical_devices[i], &physicalDeviceProperties); |
| 3328 | assert(physicalDeviceProperties.deviceType <= VK_PHYSICAL_DEVICE_TYPE_CPU); |
| 3329 | count_device_type[physicalDeviceProperties.deviceType]++; |
| 3330 | } |
| 3331 | |
| 3332 | VkPhysicalDeviceType search_for_device_type = VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU; |
| 3333 | if (count_device_type[VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU]) { |
| 3334 | search_for_device_type = VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU; |
| 3335 | } else if (count_device_type[VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU]) { |
| 3336 | search_for_device_type = VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU; |
| 3337 | } else if (count_device_type[VK_PHYSICAL_DEVICE_TYPE_VIRTUAL_GPU]) { |
| 3338 | search_for_device_type = VK_PHYSICAL_DEVICE_TYPE_VIRTUAL_GPU; |
| 3339 | } else if (count_device_type[VK_PHYSICAL_DEVICE_TYPE_CPU]) { |
| 3340 | search_for_device_type = VK_PHYSICAL_DEVICE_TYPE_CPU; |
| 3341 | } else if (count_device_type[VK_PHYSICAL_DEVICE_TYPE_OTHER]) { |
| 3342 | search_for_device_type = VK_PHYSICAL_DEVICE_TYPE_OTHER; |
| 3343 | } |
| 3344 | |
| 3345 | for (uint32_t i = 0; i < gpu_count; i++) { |
| 3346 | vkGetPhysicalDeviceProperties(physical_devices[i], &physicalDeviceProperties); |
| 3347 | if (physicalDeviceProperties.deviceType == search_for_device_type) { |
| 3348 | demo->gpu_number = i; |
| 3349 | break; |
| 3350 | } |
| 3351 | } |
| 3352 | } |
Tony-LunarG | 27c2124 | 2021-03-11 16:28:19 -0700 | [diff] [blame] | 3353 | #endif |
Witold Baryluk | a3b988f | 2021-01-07 19:03:02 +0000 | [diff] [blame] | 3354 | assert(demo->gpu_number >= 0); |
| 3355 | demo->gpu = physical_devices[demo->gpu_number]; |
| 3356 | { |
| 3357 | VkPhysicalDeviceProperties physicalDeviceProperties; |
| 3358 | vkGetPhysicalDeviceProperties(demo->gpu, &physicalDeviceProperties); |
| 3359 | fprintf(stderr, "Selected GPU %d: %s, type: %u\n", demo->gpu_number, physicalDeviceProperties.deviceName, |
| 3360 | physicalDeviceProperties.deviceType); |
| 3361 | } |
| 3362 | free(physical_devices); |
| 3363 | |
Dustin Graves | 7c28735 | 2016-01-27 13:48:06 -0700 | [diff] [blame] | 3364 | /* Look for device extensions */ |
Courtney Goeltzenleuchter | be10336 | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 3365 | uint32_t device_extension_count = 0; |
Dustin Graves | 7c28735 | 2016-01-27 13:48:06 -0700 | [diff] [blame] | 3366 | VkBool32 swapchainExtFound = 0; |
| 3367 | demo->enabled_extension_count = 0; |
Karl Schultz | 7c50ad6 | 2016-04-01 12:07:03 -0600 | [diff] [blame] | 3368 | memset(demo->extension_names, 0, sizeof(demo->extension_names)); |
Dustin Graves | 7c28735 | 2016-01-27 13:48:06 -0700 | [diff] [blame] | 3369 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 3370 | err = vkEnumerateDeviceExtensionProperties(demo->gpu, NULL, &device_extension_count, NULL); |
Courtney Goeltzenleuchter | be10336 | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 3371 | assert(!err); |
| 3372 | |
Dustin Graves | 7c28735 | 2016-01-27 13:48:06 -0700 | [diff] [blame] | 3373 | if (device_extension_count > 0) { |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 3374 | VkExtensionProperties *device_extensions = malloc(sizeof(VkExtensionProperties) * device_extension_count); |
| 3375 | err = vkEnumerateDeviceExtensionProperties(demo->gpu, NULL, &device_extension_count, device_extensions); |
Dustin Graves | 7c28735 | 2016-01-27 13:48:06 -0700 | [diff] [blame] | 3376 | assert(!err); |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 3377 | |
Dustin Graves | 7c28735 | 2016-01-27 13:48:06 -0700 | [diff] [blame] | 3378 | for (uint32_t i = 0; i < device_extension_count; i++) { |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 3379 | if (!strcmp(VK_KHR_SWAPCHAIN_EXTENSION_NAME, device_extensions[i].extensionName)) { |
Dustin Graves | 7c28735 | 2016-01-27 13:48:06 -0700 | [diff] [blame] | 3380 | swapchainExtFound = 1; |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 3381 | demo->extension_names[demo->enabled_extension_count++] = VK_KHR_SWAPCHAIN_EXTENSION_NAME; |
Dustin Graves | 7c28735 | 2016-01-27 13:48:06 -0700 | [diff] [blame] | 3382 | } |
Richard S. Wright Jr | a782574 | 2021-01-06 16:02:12 -0500 | [diff] [blame] | 3383 | if (!strcmp("VK_KHR_portability_subset", device_extensions[i].extensionName)) { |
| 3384 | demo->extension_names[demo->enabled_extension_count++] = "VK_KHR_portability_subset"; |
Richard S. Wright Jr | ca51bc7 | 2021-01-06 13:03:18 -0500 | [diff] [blame] | 3385 | } |
Dustin Graves | 7c28735 | 2016-01-27 13:48:06 -0700 | [diff] [blame] | 3386 | assert(demo->enabled_extension_count < 64); |
Courtney Goeltzenleuchter | be10336 | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 3387 | } |
Dustin Graves | 7c28735 | 2016-01-27 13:48:06 -0700 | [diff] [blame] | 3388 | |
Ian Elliott | 53622a7 | 2017-03-28 11:06:33 -0600 | [diff] [blame] | 3389 | if (demo->VK_KHR_incremental_present_enabled) { |
| 3390 | // Even though the user "enabled" the extension via the command |
| 3391 | // line, we must make sure that it's enumerated for use with the |
| 3392 | // device. Therefore, disable it here, and re-enable it again if |
| 3393 | // enumerated. |
| 3394 | demo->VK_KHR_incremental_present_enabled = false; |
| 3395 | for (uint32_t i = 0; i < device_extension_count; i++) { |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 3396 | if (!strcmp(VK_KHR_INCREMENTAL_PRESENT_EXTENSION_NAME, device_extensions[i].extensionName)) { |
| 3397 | demo->extension_names[demo->enabled_extension_count++] = VK_KHR_INCREMENTAL_PRESENT_EXTENSION_NAME; |
Ian Elliott | 53622a7 | 2017-03-28 11:06:33 -0600 | [diff] [blame] | 3398 | demo->VK_KHR_incremental_present_enabled = true; |
| 3399 | DbgMsg("VK_KHR_incremental_present extension enabled\n"); |
| 3400 | } |
| 3401 | assert(demo->enabled_extension_count < 64); |
| 3402 | } |
| 3403 | if (!demo->VK_KHR_incremental_present_enabled) { |
| 3404 | DbgMsg("VK_KHR_incremental_present extension NOT AVAILABLE\n"); |
| 3405 | } |
| 3406 | } |
| 3407 | |
Ian Elliott | d940ca2 | 2017-03-22 08:31:27 -0600 | [diff] [blame] | 3408 | if (demo->VK_GOOGLE_display_timing_enabled) { |
| 3409 | // Even though the user "enabled" the extension via the command |
| 3410 | // line, we must make sure that it's enumerated for use with the |
| 3411 | // device. Therefore, disable it here, and re-enable it again if |
| 3412 | // enumerated. |
| 3413 | demo->VK_GOOGLE_display_timing_enabled = false; |
| 3414 | for (uint32_t i = 0; i < device_extension_count; i++) { |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 3415 | if (!strcmp(VK_GOOGLE_DISPLAY_TIMING_EXTENSION_NAME, device_extensions[i].extensionName)) { |
| 3416 | demo->extension_names[demo->enabled_extension_count++] = VK_GOOGLE_DISPLAY_TIMING_EXTENSION_NAME; |
Ian Elliott | d940ca2 | 2017-03-22 08:31:27 -0600 | [diff] [blame] | 3417 | demo->VK_GOOGLE_display_timing_enabled = true; |
Ian Elliott | 0c7b3c9 | 2017-03-27 14:38:52 -0600 | [diff] [blame] | 3418 | DbgMsg("VK_GOOGLE_display_timing extension enabled\n"); |
Ian Elliott | d940ca2 | 2017-03-22 08:31:27 -0600 | [diff] [blame] | 3419 | } |
| 3420 | assert(demo->enabled_extension_count < 64); |
| 3421 | } |
| 3422 | if (!demo->VK_GOOGLE_display_timing_enabled) { |
Ian Elliott | 0c7b3c9 | 2017-03-27 14:38:52 -0600 | [diff] [blame] | 3423 | DbgMsg("VK_GOOGLE_display_timing extension NOT AVAILABLE\n"); |
Ian Elliott | d940ca2 | 2017-03-22 08:31:27 -0600 | [diff] [blame] | 3424 | } |
| 3425 | } |
| 3426 | |
Dustin Graves | 7c28735 | 2016-01-27 13:48:06 -0700 | [diff] [blame] | 3427 | free(device_extensions); |
Courtney Goeltzenleuchter | be10336 | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 3428 | } |
Dustin Graves | 7c28735 | 2016-01-27 13:48:06 -0700 | [diff] [blame] | 3429 | |
Tony Barbour | cc69f45 | 2015-10-08 13:59:42 -0600 | [diff] [blame] | 3430 | if (!swapchainExtFound) { |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 3431 | ERR_EXIT("vkEnumerateDeviceExtensionProperties failed to find the " VK_KHR_SWAPCHAIN_EXTENSION_NAME |
| 3432 | " extension.\n\nDo you have a compatible Vulkan installable client driver (ICD) installed?\n" |
| 3433 | "Please look at the Getting Started guide for additional information.\n", |
Courtney Goeltzenleuchter | be10336 | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 3434 | "vkCreateInstance Failure"); |
| 3435 | } |
| 3436 | |
Tony Barbour | 3dddd5d | 2015-04-29 16:19:20 -0600 | [diff] [blame] | 3437 | if (demo->validate) { |
Mark Young | 66510e5 | 2017-11-10 10:05:14 -0700 | [diff] [blame] | 3438 | // Setup VK_EXT_debug_utils function pointers always (we use them for |
| 3439 | // debug labels and names). |
| 3440 | demo->CreateDebugUtilsMessengerEXT = |
| 3441 | (PFN_vkCreateDebugUtilsMessengerEXT)vkGetInstanceProcAddr(demo->inst, "vkCreateDebugUtilsMessengerEXT"); |
| 3442 | demo->DestroyDebugUtilsMessengerEXT = |
| 3443 | (PFN_vkDestroyDebugUtilsMessengerEXT)vkGetInstanceProcAddr(demo->inst, "vkDestroyDebugUtilsMessengerEXT"); |
| 3444 | demo->SubmitDebugUtilsMessageEXT = |
| 3445 | (PFN_vkSubmitDebugUtilsMessageEXT)vkGetInstanceProcAddr(demo->inst, "vkSubmitDebugUtilsMessageEXT"); |
| 3446 | demo->CmdBeginDebugUtilsLabelEXT = |
| 3447 | (PFN_vkCmdBeginDebugUtilsLabelEXT)vkGetInstanceProcAddr(demo->inst, "vkCmdBeginDebugUtilsLabelEXT"); |
| 3448 | demo->CmdEndDebugUtilsLabelEXT = |
| 3449 | (PFN_vkCmdEndDebugUtilsLabelEXT)vkGetInstanceProcAddr(demo->inst, "vkCmdEndDebugUtilsLabelEXT"); |
| 3450 | demo->CmdInsertDebugUtilsLabelEXT = |
| 3451 | (PFN_vkCmdInsertDebugUtilsLabelEXT)vkGetInstanceProcAddr(demo->inst, "vkCmdInsertDebugUtilsLabelEXT"); |
| 3452 | demo->SetDebugUtilsObjectNameEXT = |
| 3453 | (PFN_vkSetDebugUtilsObjectNameEXT)vkGetInstanceProcAddr(demo->inst, "vkSetDebugUtilsObjectNameEXT"); |
| 3454 | if (NULL == demo->CreateDebugUtilsMessengerEXT || NULL == demo->DestroyDebugUtilsMessengerEXT || |
| 3455 | NULL == demo->SubmitDebugUtilsMessageEXT || NULL == demo->CmdBeginDebugUtilsLabelEXT || |
| 3456 | NULL == demo->CmdEndDebugUtilsLabelEXT || NULL == demo->CmdInsertDebugUtilsLabelEXT || |
| 3457 | NULL == demo->SetDebugUtilsObjectNameEXT) { |
| 3458 | ERR_EXIT("GetProcAddr: Failed to init VK_EXT_debug_utils\n", "GetProcAddr: Failure"); |
Courtney Goeltzenleuchter | af1951c | 2015-12-01 14:11:38 -0700 | [diff] [blame] | 3459 | } |
Jon Ashburn | 9549c8e | 2015-12-15 08:47:46 -0700 | [diff] [blame] | 3460 | |
Mark Young | 66510e5 | 2017-11-10 10:05:14 -0700 | [diff] [blame] | 3461 | err = demo->CreateDebugUtilsMessengerEXT(demo->inst, &dbg_messenger_create_info, NULL, &demo->dbg_messenger); |
Courtney Goeltzenleuchter | 922a0fa | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 3462 | switch (err) { |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 3463 | case VK_SUCCESS: |
| 3464 | break; |
| 3465 | case VK_ERROR_OUT_OF_HOST_MEMORY: |
Mark Young | 66510e5 | 2017-11-10 10:05:14 -0700 | [diff] [blame] | 3466 | ERR_EXIT("CreateDebugUtilsMessengerEXT: out of host memory\n", "CreateDebugUtilsMessengerEXT Failure"); |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 3467 | break; |
| 3468 | default: |
Mark Young | 66510e5 | 2017-11-10 10:05:14 -0700 | [diff] [blame] | 3469 | ERR_EXIT("CreateDebugUtilsMessengerEXT: unknown failure\n", "CreateDebugUtilsMessengerEXT Failure"); |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 3470 | break; |
Courtney Goeltzenleuchter | 922a0fa | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 3471 | } |
Tony Barbour | 3dddd5d | 2015-04-29 16:19:20 -0600 | [diff] [blame] | 3472 | } |
Courtney Goeltzenleuchter | bd5c174 | 2015-10-20 16:40:38 -0600 | [diff] [blame] | 3473 | vkGetPhysicalDeviceProperties(demo->gpu, &demo->gpu_props); |
Tobin Ehlis | 9626ba6 | 2015-09-22 13:52:37 -0600 | [diff] [blame] | 3474 | |
| 3475 | /* Call with NULL data to get count */ |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 3476 | vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_family_count, NULL); |
Tony Barbour | ab2a036 | 2016-09-06 11:40:12 -0600 | [diff] [blame] | 3477 | assert(demo->queue_family_count >= 1); |
Tobin Ehlis | 9626ba6 | 2015-09-22 13:52:37 -0600 | [diff] [blame] | 3478 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 3479 | demo->queue_props = (VkQueueFamilyProperties *)malloc(demo->queue_family_count * sizeof(VkQueueFamilyProperties)); |
| 3480 | vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_family_count, demo->queue_props); |
Tony Barbour | ab2a036 | 2016-09-06 11:40:12 -0600 | [diff] [blame] | 3481 | |
Tobin Ehlis | bf3020e | 2015-09-24 15:18:22 -0600 | [diff] [blame] | 3482 | // Query fine-grained feature support for this device. |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 3483 | // If app has specific feature requirements it should check supported |
| 3484 | // features based on this query |
Tobin Ehlis | 8d03b7c | 2015-09-29 11:22:37 -0600 | [diff] [blame] | 3485 | VkPhysicalDeviceFeatures physDevFeatures; |
Courtney Goeltzenleuchter | bd5c174 | 2015-10-20 16:40:38 -0600 | [diff] [blame] | 3486 | vkGetPhysicalDeviceFeatures(demo->gpu, &physDevFeatures); |
Tobin Ehlis | bf3020e | 2015-09-24 15:18:22 -0600 | [diff] [blame] | 3487 | |
Tony Barbour | da2bad5 | 2016-01-22 14:36:40 -0700 | [diff] [blame] | 3488 | GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceSupportKHR); |
| 3489 | GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceCapabilitiesKHR); |
| 3490 | GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceFormatsKHR); |
| 3491 | GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfacePresentModesKHR); |
| 3492 | GET_INSTANCE_PROC_ADDR(demo->inst, GetSwapchainImagesKHR); |
| 3493 | } |
| 3494 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 3495 | static void demo_create_device(struct demo *demo) { |
Tony Barbour | da2bad5 | 2016-01-22 14:36:40 -0700 | [diff] [blame] | 3496 | VkResult U_ASSERT_ONLY err; |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 3497 | float queue_priorities[1] = {0.0}; |
Tony Barbour | 22e0627 | 2016-09-07 16:07:56 -0600 | [diff] [blame] | 3498 | VkDeviceQueueCreateInfo queues[2]; |
| 3499 | queues[0].sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO; |
| 3500 | queues[0].pNext = NULL; |
| 3501 | queues[0].queueFamilyIndex = demo->graphics_queue_family_index; |
| 3502 | queues[0].queueCount = 1; |
| 3503 | queues[0].pQueuePriorities = queue_priorities; |
| 3504 | queues[0].flags = 0; |
Tobin Ehlis | 9626ba6 | 2015-09-22 13:52:37 -0600 | [diff] [blame] | 3505 | |
| 3506 | VkDeviceCreateInfo device = { |
| 3507 | .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO, |
| 3508 | .pNext = NULL, |
Chia-I Wu | 8b49744 | 2015-11-06 06:42:02 +0800 | [diff] [blame] | 3509 | .queueCreateInfoCount = 1, |
Tony Barbour | 22e0627 | 2016-09-07 16:07:56 -0600 | [diff] [blame] | 3510 | .pQueueCreateInfos = queues, |
Karl Schultz | 5b423ea | 2016-06-20 19:08:43 -0600 | [diff] [blame] | 3511 | .enabledLayerCount = 0, |
| 3512 | .ppEnabledLayerNames = NULL, |
Tony Barbour | da2bad5 | 2016-01-22 14:36:40 -0700 | [diff] [blame] | 3513 | .enabledExtensionCount = demo->enabled_extension_count, |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 3514 | .ppEnabledExtensionNames = (const char *const *)demo->extension_names, |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 3515 | .pEnabledFeatures = NULL, // If specific features are required, pass them in here |
Tobin Ehlis | 9626ba6 | 2015-09-22 13:52:37 -0600 | [diff] [blame] | 3516 | }; |
Tony Barbour | 22e0627 | 2016-09-07 16:07:56 -0600 | [diff] [blame] | 3517 | if (demo->separate_present_queue) { |
| 3518 | queues[1].sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO; |
| 3519 | queues[1].pNext = NULL; |
| 3520 | queues[1].queueFamilyIndex = demo->present_queue_family_index; |
| 3521 | queues[1].queueCount = 1; |
| 3522 | queues[1].pQueuePriorities = queue_priorities; |
| 3523 | queues[1].flags = 0; |
| 3524 | device.queueCreateInfoCount = 2; |
| 3525 | } |
Chia-I Wu | 6363606 | 2015-10-26 21:10:41 +0800 | [diff] [blame] | 3526 | err = vkCreateDevice(demo->gpu, &device, NULL, &demo->device); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 3527 | assert(!err); |
Courtney Goeltzenleuchter | 3a35c82 | 2015-07-15 17:45:38 -0600 | [diff] [blame] | 3528 | } |
| 3529 | |
Tony-LunarG | 6cebf14 | 2019-09-11 14:32:23 -0600 | [diff] [blame] | 3530 | static void demo_create_surface(struct demo *demo) { |
Courtney Goeltzenleuchter | d3cdaaf | 2015-12-17 09:53:29 -0700 | [diff] [blame] | 3531 | VkResult U_ASSERT_ONLY err; |
Courtney Goeltzenleuchter | ed667a5 | 2015-03-05 18:09:39 -0700 | [diff] [blame] | 3532 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 3533 | // Create a WSI surface for the window: |
Michael Lentine | c6cde4b | 2016-04-18 13:20:45 -0500 | [diff] [blame] | 3534 | #if defined(VK_USE_PLATFORM_WIN32_KHR) |
Ian Elliott | a7a731c | 2015-12-11 15:52:12 -0700 | [diff] [blame] | 3535 | VkWin32SurfaceCreateInfoKHR createInfo; |
| 3536 | createInfo.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR; |
| 3537 | createInfo.pNext = NULL; |
| 3538 | createInfo.flags = 0; |
Jon Ashburn | b90f50d | 2015-12-24 17:08:01 -0700 | [diff] [blame] | 3539 | createInfo.hinstance = demo->connection; |
| 3540 | createInfo.hwnd = demo->window; |
Ian Elliott | a7a731c | 2015-12-11 15:52:12 -0700 | [diff] [blame] | 3541 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 3542 | err = vkCreateWin32SurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface); |
Tony Barbour | 153cb06 | 2016-12-07 13:43:36 -0700 | [diff] [blame] | 3543 | #elif defined(VK_USE_PLATFORM_WAYLAND_KHR) |
Mun Gwan-gyeong | 6f01c55 | 2016-06-27 05:34:44 +0900 | [diff] [blame] | 3544 | VkWaylandSurfaceCreateInfoKHR createInfo; |
| 3545 | createInfo.sType = VK_STRUCTURE_TYPE_WAYLAND_SURFACE_CREATE_INFO_KHR; |
| 3546 | createInfo.pNext = NULL; |
| 3547 | createInfo.flags = 0; |
| 3548 | createInfo.display = demo->display; |
| 3549 | createInfo.surface = demo->window; |
| 3550 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 3551 | err = vkCreateWaylandSurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface); |
Michael Lentine | c6cde4b | 2016-04-18 13:20:45 -0500 | [diff] [blame] | 3552 | #elif defined(VK_USE_PLATFORM_ANDROID_KHR) |
| 3553 | VkAndroidSurfaceCreateInfoKHR createInfo; |
| 3554 | createInfo.sType = VK_STRUCTURE_TYPE_ANDROID_SURFACE_CREATE_INFO_KHR; |
| 3555 | createInfo.pNext = NULL; |
| 3556 | createInfo.flags = 0; |
Mike Schuchardt | 837d516 | 2018-03-08 12:57:02 -0700 | [diff] [blame] | 3557 | createInfo.window = (struct ANativeWindow *)(demo->window); |
Ian Elliott | b08e0d9 | 2015-11-20 11:55:46 -0700 | [diff] [blame] | 3558 | |
Michael Lentine | c6cde4b | 2016-04-18 13:20:45 -0500 | [diff] [blame] | 3559 | err = vkCreateAndroidSurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface); |
Tony Barbour | 153cb06 | 2016-12-07 13:43:36 -0700 | [diff] [blame] | 3560 | #elif defined(VK_USE_PLATFORM_XLIB_KHR) |
| 3561 | VkXlibSurfaceCreateInfoKHR createInfo; |
| 3562 | createInfo.sType = VK_STRUCTURE_TYPE_XLIB_SURFACE_CREATE_INFO_KHR; |
| 3563 | createInfo.pNext = NULL; |
| 3564 | createInfo.flags = 0; |
| 3565 | createInfo.dpy = demo->display; |
| 3566 | createInfo.window = demo->xlib_window; |
Ian Elliott | a7a731c | 2015-12-11 15:52:12 -0700 | [diff] [blame] | 3567 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 3568 | err = vkCreateXlibSurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface); |
Tony Barbour | 153cb06 | 2016-12-07 13:43:36 -0700 | [diff] [blame] | 3569 | #elif defined(VK_USE_PLATFORM_XCB_KHR) |
| 3570 | VkXcbSurfaceCreateInfoKHR createInfo; |
| 3571 | createInfo.sType = VK_STRUCTURE_TYPE_XCB_SURFACE_CREATE_INFO_KHR; |
| 3572 | createInfo.pNext = NULL; |
| 3573 | createInfo.flags = 0; |
| 3574 | createInfo.connection = demo->connection; |
| 3575 | createInfo.window = demo->xcb_window; |
Tony Barbour | 2593b18 | 2016-04-19 10:57:58 -0600 | [diff] [blame] | 3576 | |
Tony Barbour | 153cb06 | 2016-12-07 13:43:36 -0700 | [diff] [blame] | 3577 | err = vkCreateXcbSurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface); |
Nicolas Caramelli | c8be8c8 | 2020-07-13 17:22:09 +0200 | [diff] [blame] | 3578 | #elif defined(VK_USE_PLATFORM_DIRECTFB_EXT) |
| 3579 | VkDirectFBSurfaceCreateInfoEXT createInfo; |
| 3580 | createInfo.sType = VK_STRUCTURE_TYPE_DIRECTFB_SURFACE_CREATE_INFO_EXT; |
| 3581 | createInfo.pNext = NULL; |
| 3582 | createInfo.flags = 0; |
| 3583 | createInfo.dfb = demo->dfb; |
| 3584 | createInfo.surface = demo->window; |
| 3585 | |
| 3586 | err = vkCreateDirectFBSurfaceEXT(demo->inst, &createInfo, NULL, &demo->surface); |
Damien Leone | 600c305 | 2017-01-31 10:26:07 -0700 | [diff] [blame] | 3587 | #elif defined(VK_USE_PLATFORM_DISPLAY_KHR) |
| 3588 | err = demo_create_display_surface(demo); |
Bill Hollings | 0a0625a | 2019-07-15 17:39:18 -0400 | [diff] [blame] | 3589 | #elif defined(VK_USE_PLATFORM_METAL_EXT) |
| 3590 | VkMetalSurfaceCreateInfoEXT surface; |
| 3591 | surface.sType = VK_STRUCTURE_TYPE_METAL_SURFACE_CREATE_INFO_EXT; |
Bill Hollings | f435214 | 2017-02-14 22:58:56 -0500 | [diff] [blame] | 3592 | surface.pNext = NULL; |
| 3593 | surface.flags = 0; |
Bill Hollings | 0a0625a | 2019-07-15 17:39:18 -0400 | [diff] [blame] | 3594 | surface.pLayer = demo->caMetalLayer; |
Bill Hollings | f435214 | 2017-02-14 22:58:56 -0500 | [diff] [blame] | 3595 | |
Bill Hollings | 0a0625a | 2019-07-15 17:39:18 -0400 | [diff] [blame] | 3596 | err = vkCreateMetalSurfaceEXT(demo->inst, &surface, NULL, &demo->surface); |
Michael Lentine | c6cde4b | 2016-04-18 13:20:45 -0500 | [diff] [blame] | 3597 | #endif |
Tony Barbour | 2593b18 | 2016-04-19 10:57:58 -0600 | [diff] [blame] | 3598 | assert(!err); |
Tony-LunarG | 6cebf14 | 2019-09-11 14:32:23 -0600 | [diff] [blame] | 3599 | } |
| 3600 | |
Joshua Ashton | 1fbefe0 | 2020-04-02 06:45:48 +0100 | [diff] [blame] | 3601 | static VkSurfaceFormatKHR pick_surface_format(const VkSurfaceFormatKHR *surfaceFormats, uint32_t count) { |
| 3602 | // Prefer non-SRGB formats... |
| 3603 | for (uint32_t i = 0; i < count; i++) { |
| 3604 | const VkFormat format = surfaceFormats[i].format; |
| 3605 | |
| 3606 | if (format == VK_FORMAT_R8G8B8A8_UNORM || format == VK_FORMAT_B8G8R8A8_UNORM || |
| 3607 | format == VK_FORMAT_A2B10G10R10_UNORM_PACK32 || format == VK_FORMAT_A2R10G10B10_UNORM_PACK32 || |
| 3608 | format == VK_FORMAT_R16G16B16A16_SFLOAT) { |
| 3609 | return surfaceFormats[i]; |
| 3610 | } |
| 3611 | } |
| 3612 | |
| 3613 | printf("Can't find our preferred formats... Falling back to first exposed format. Rendering may be incorrect.\n"); |
| 3614 | |
| 3615 | assert(count >= 1); |
| 3616 | return surfaceFormats[0]; |
| 3617 | } |
| 3618 | |
Tony-LunarG | 6cebf14 | 2019-09-11 14:32:23 -0600 | [diff] [blame] | 3619 | static void demo_init_vk_swapchain(struct demo *demo) { |
| 3620 | VkResult U_ASSERT_ONLY err; |
| 3621 | |
| 3622 | demo_create_surface(demo); |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 3623 | |
Tony Barbour | cc69f45 | 2015-10-08 13:59:42 -0600 | [diff] [blame] | 3624 | // Iterate over each queue to learn whether it supports presenting: |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 3625 | VkBool32 *supportsPresent = (VkBool32 *)malloc(demo->queue_family_count * sizeof(VkBool32)); |
Karl Schultz | a261546 | 2017-01-20 13:19:20 -0700 | [diff] [blame] | 3626 | for (uint32_t i = 0; i < demo->queue_family_count; i++) { |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 3627 | demo->fpGetPhysicalDeviceSurfaceSupportKHR(demo->gpu, i, demo->surface, &supportsPresent[i]); |
Courtney Goeltzenleuchter | ed667a5 | 2015-03-05 18:09:39 -0700 | [diff] [blame] | 3628 | } |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 3629 | |
| 3630 | // Search for a graphics and a present queue in the array of queue |
| 3631 | // families, try to find one that supports both |
Tony Barbour | ab2a036 | 2016-09-06 11:40:12 -0600 | [diff] [blame] | 3632 | uint32_t graphicsQueueFamilyIndex = UINT32_MAX; |
| 3633 | uint32_t presentQueueFamilyIndex = UINT32_MAX; |
Karl Schultz | a261546 | 2017-01-20 13:19:20 -0700 | [diff] [blame] | 3634 | for (uint32_t i = 0; i < demo->queue_family_count; i++) { |
Tony Barbour | ab2a036 | 2016-09-06 11:40:12 -0600 | [diff] [blame] | 3635 | if ((demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0) { |
| 3636 | if (graphicsQueueFamilyIndex == UINT32_MAX) { |
| 3637 | graphicsQueueFamilyIndex = i; |
| 3638 | } |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 3639 | |
Tony Barbour | ab2a036 | 2016-09-06 11:40:12 -0600 | [diff] [blame] | 3640 | if (supportsPresent[i] == VK_TRUE) { |
| 3641 | graphicsQueueFamilyIndex = i; |
| 3642 | presentQueueFamilyIndex = i; |
| 3643 | break; |
| 3644 | } |
| 3645 | } |
| 3646 | } |
| 3647 | |
| 3648 | if (presentQueueFamilyIndex == UINT32_MAX) { |
| 3649 | // If didn't find a queue that supports both graphics and present, then |
| 3650 | // find a separate present queue. |
Karl Schultz | a261546 | 2017-01-20 13:19:20 -0700 | [diff] [blame] | 3651 | for (uint32_t i = 0; i < demo->queue_family_count; ++i) { |
Tony Barbour | ab2a036 | 2016-09-06 11:40:12 -0600 | [diff] [blame] | 3652 | if (supportsPresent[i] == VK_TRUE) { |
| 3653 | presentQueueFamilyIndex = i; |
| 3654 | break; |
| 3655 | } |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 3656 | } |
| 3657 | } |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 3658 | |
| 3659 | // Generate error if could not find both a graphics and a present queue |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 3660 | if (graphicsQueueFamilyIndex == UINT32_MAX || presentQueueFamilyIndex == UINT32_MAX) { |
| 3661 | ERR_EXIT("Could not find both graphics and present queues\n", "Swapchain Initialization Failure"); |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 3662 | } |
| 3663 | |
Tony Barbour | ab2a036 | 2016-09-06 11:40:12 -0600 | [diff] [blame] | 3664 | demo->graphics_queue_family_index = graphicsQueueFamilyIndex; |
| 3665 | demo->present_queue_family_index = presentQueueFamilyIndex; |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 3666 | demo->separate_present_queue = (demo->graphics_queue_family_index != demo->present_queue_family_index); |
Tony Barbour | ab2a036 | 2016-09-06 11:40:12 -0600 | [diff] [blame] | 3667 | free(supportsPresent); |
Courtney Goeltzenleuchter | ed667a5 | 2015-03-05 18:09:39 -0700 | [diff] [blame] | 3668 | |
Tony Barbour | da2bad5 | 2016-01-22 14:36:40 -0700 | [diff] [blame] | 3669 | demo_create_device(demo); |
| 3670 | |
| 3671 | GET_DEVICE_PROC_ADDR(demo->device, CreateSwapchainKHR); |
| 3672 | GET_DEVICE_PROC_ADDR(demo->device, DestroySwapchainKHR); |
| 3673 | GET_DEVICE_PROC_ADDR(demo->device, GetSwapchainImagesKHR); |
| 3674 | GET_DEVICE_PROC_ADDR(demo->device, AcquireNextImageKHR); |
| 3675 | GET_DEVICE_PROC_ADDR(demo->device, QueuePresentKHR); |
Ian Elliott | d940ca2 | 2017-03-22 08:31:27 -0600 | [diff] [blame] | 3676 | if (demo->VK_GOOGLE_display_timing_enabled) { |
| 3677 | GET_DEVICE_PROC_ADDR(demo->device, GetRefreshCycleDurationGOOGLE); |
| 3678 | GET_DEVICE_PROC_ADDR(demo->device, GetPastPresentationTimingGOOGLE); |
| 3679 | } |
Tony Barbour | da2bad5 | 2016-01-22 14:36:40 -0700 | [diff] [blame] | 3680 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 3681 | vkGetDeviceQueue(demo->device, demo->graphics_queue_family_index, 0, &demo->graphics_queue); |
Tony Barbour | a2a6781 | 2016-07-18 13:21:06 -0600 | [diff] [blame] | 3682 | |
Tony Barbour | 22e0627 | 2016-09-07 16:07:56 -0600 | [diff] [blame] | 3683 | if (!demo->separate_present_queue) { |
Tony Barbour | a2a6781 | 2016-07-18 13:21:06 -0600 | [diff] [blame] | 3684 | demo->present_queue = demo->graphics_queue; |
| 3685 | } else { |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 3686 | vkGetDeviceQueue(demo->device, demo->present_queue_family_index, 0, &demo->present_queue); |
Tony Barbour | a2a6781 | 2016-07-18 13:21:06 -0600 | [diff] [blame] | 3687 | } |
Tony Barbour | 3dddd5d | 2015-04-29 16:19:20 -0600 | [diff] [blame] | 3688 | |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 3689 | // Get the list of VkFormat's that are supported: |
Ian Elliott | 8528eff | 2015-08-07 15:56:59 -0600 | [diff] [blame] | 3690 | uint32_t formatCount; |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 3691 | err = demo->fpGetPhysicalDeviceSurfaceFormatsKHR(demo->gpu, demo->surface, &formatCount, NULL); |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 3692 | assert(!err); |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 3693 | VkSurfaceFormatKHR *surfFormats = (VkSurfaceFormatKHR *)malloc(formatCount * sizeof(VkSurfaceFormatKHR)); |
| 3694 | err = demo->fpGetPhysicalDeviceSurfaceFormatsKHR(demo->gpu, demo->surface, &formatCount, surfFormats); |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 3695 | assert(!err); |
Joshua Ashton | 1fbefe0 | 2020-04-02 06:45:48 +0100 | [diff] [blame] | 3696 | VkSurfaceFormatKHR surfaceFormat = pick_surface_format(surfFormats, formatCount); |
| 3697 | demo->format = surfaceFormat.format; |
| 3698 | demo->color_space = surfaceFormat.colorSpace; |
Jason Chen | 5d4fee9 | 2019-04-04 12:45:29 +0800 | [diff] [blame] | 3699 | free(surfFormats); |
Ian Elliott | e4602cd | 2015-04-21 16:41:02 -0600 | [diff] [blame] | 3700 | |
David Pinedo | 2bb7c93 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 3701 | demo->quit = false; |
| 3702 | demo->curFrame = 0; |
Mark Lobodzinski | eadf998 | 2015-07-02 16:49:40 -0600 | [diff] [blame] | 3703 | |
Tony Barbour | ce7524e | 2016-07-28 12:01:45 -0600 | [diff] [blame] | 3704 | // Create semaphores to synchronize acquiring presentable buffers before |
| 3705 | // rendering and waiting for drawing to be complete before presenting |
| 3706 | VkSemaphoreCreateInfo semaphoreCreateInfo = { |
| 3707 | .sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO, |
| 3708 | .pNext = NULL, |
| 3709 | .flags = 0, |
| 3710 | }; |
Tony Barbour | ce7524e | 2016-07-28 12:01:45 -0600 | [diff] [blame] | 3711 | |
Tony Barbour | 66a56c3 | 2016-09-21 13:10:56 -0600 | [diff] [blame] | 3712 | // Create fences that we can use to throttle if we get too far |
| 3713 | // ahead of the image presents |
| 3714 | VkFenceCreateInfo fence_ci = { |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 3715 | .sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO, .pNext = NULL, .flags = VK_FENCE_CREATE_SIGNALED_BIT}; |
Tony Barbour | 66a56c3 | 2016-09-21 13:10:56 -0600 | [diff] [blame] | 3716 | for (uint32_t i = 0; i < FRAME_LAG; i++) { |
Tony Barbour | a72d949 | 2017-01-11 13:35:09 -0700 | [diff] [blame] | 3717 | err = vkCreateFence(demo->device, &fence_ci, NULL, &demo->fences[i]); |
| 3718 | assert(!err); |
| 3719 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 3720 | err = vkCreateSemaphore(demo->device, &semaphoreCreateInfo, NULL, &demo->image_acquired_semaphores[i]); |
Tony Barbour | 22e0627 | 2016-09-07 16:07:56 -0600 | [diff] [blame] | 3721 | assert(!err); |
Tony Barbour | 66a56c3 | 2016-09-21 13:10:56 -0600 | [diff] [blame] | 3722 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 3723 | err = vkCreateSemaphore(demo->device, &semaphoreCreateInfo, NULL, &demo->draw_complete_semaphores[i]); |
Tony Barbour | 66a56c3 | 2016-09-21 13:10:56 -0600 | [diff] [blame] | 3724 | assert(!err); |
| 3725 | |
| 3726 | if (demo->separate_present_queue) { |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 3727 | err = vkCreateSemaphore(demo->device, &semaphoreCreateInfo, NULL, &demo->image_ownership_semaphores[i]); |
Tony Barbour | 66a56c3 | 2016-09-21 13:10:56 -0600 | [diff] [blame] | 3728 | assert(!err); |
| 3729 | } |
Tony Barbour | 22e0627 | 2016-09-07 16:07:56 -0600 | [diff] [blame] | 3730 | } |
Tony Barbour | 66a56c3 | 2016-09-21 13:10:56 -0600 | [diff] [blame] | 3731 | demo->frame_index = 0; |
Tony Barbour | 22e0627 | 2016-09-07 16:07:56 -0600 | [diff] [blame] | 3732 | |
Mark Lobodzinski | eadf998 | 2015-07-02 16:49:40 -0600 | [diff] [blame] | 3733 | // Get Memory information and properties |
Courtney Goeltzenleuchter | bd5c174 | 2015-10-20 16:40:38 -0600 | [diff] [blame] | 3734 | vkGetPhysicalDeviceMemoryProperties(demo->gpu, &demo->memory_properties); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 3735 | } |
| 3736 | |
Tony Barbour | 153cb06 | 2016-12-07 13:43:36 -0700 | [diff] [blame] | 3737 | #if defined(VK_USE_PLATFORM_WAYLAND_KHR) |
Joey Bzdek | 15eb070 | 2017-06-07 09:40:36 -0600 | [diff] [blame] | 3738 | static void pointer_handle_enter(void *data, struct wl_pointer *pointer, uint32_t serial, struct wl_surface *surface, wl_fixed_t sx, |
| 3739 | wl_fixed_t sy) {} |
| 3740 | |
| 3741 | static void pointer_handle_leave(void *data, struct wl_pointer *pointer, uint32_t serial, struct wl_surface *surface) {} |
| 3742 | |
| 3743 | static void pointer_handle_motion(void *data, struct wl_pointer *pointer, uint32_t time, wl_fixed_t sx, wl_fixed_t sy) {} |
| 3744 | |
| 3745 | static void pointer_handle_button(void *data, struct wl_pointer *wl_pointer, uint32_t serial, uint32_t time, uint32_t button, |
| 3746 | uint32_t state) { |
| 3747 | struct demo *demo = data; |
| 3748 | if (button == BTN_LEFT && state == WL_POINTER_BUTTON_STATE_PRESSED) { |
Tony-LunarG | 5ceb7be | 2019-12-05 16:05:40 -0700 | [diff] [blame] | 3749 | wl_shell_surface_move(demo->shell_surface, demo->seat, serial); |
Joey Bzdek | 15eb070 | 2017-06-07 09:40:36 -0600 | [diff] [blame] | 3750 | } |
| 3751 | } |
| 3752 | |
| 3753 | static void pointer_handle_axis(void *data, struct wl_pointer *wl_pointer, uint32_t time, uint32_t axis, wl_fixed_t value) {} |
| 3754 | |
| 3755 | static const struct wl_pointer_listener pointer_listener = { |
| 3756 | pointer_handle_enter, pointer_handle_leave, pointer_handle_motion, pointer_handle_button, pointer_handle_axis, |
| 3757 | }; |
| 3758 | |
| 3759 | static void keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard, uint32_t format, int fd, uint32_t size) {} |
| 3760 | |
| 3761 | static void keyboard_handle_enter(void *data, struct wl_keyboard *keyboard, uint32_t serial, struct wl_surface *surface, |
| 3762 | struct wl_array *keys) {} |
| 3763 | |
| 3764 | static void keyboard_handle_leave(void *data, struct wl_keyboard *keyboard, uint32_t serial, struct wl_surface *surface) {} |
| 3765 | |
| 3766 | static void keyboard_handle_key(void *data, struct wl_keyboard *keyboard, uint32_t serial, uint32_t time, uint32_t key, |
| 3767 | uint32_t state) { |
| 3768 | if (state != WL_KEYBOARD_KEY_STATE_RELEASED) return; |
| 3769 | struct demo *demo = data; |
| 3770 | switch (key) { |
| 3771 | case KEY_ESC: // Escape |
| 3772 | demo->quit = true; |
| 3773 | break; |
| 3774 | case KEY_LEFT: // left arrow key |
| 3775 | demo->spin_angle -= demo->spin_increment; |
| 3776 | break; |
| 3777 | case KEY_RIGHT: // right arrow key |
| 3778 | demo->spin_angle += demo->spin_increment; |
| 3779 | break; |
| 3780 | case KEY_SPACE: // space bar |
| 3781 | demo->pause = !demo->pause; |
| 3782 | break; |
| 3783 | } |
| 3784 | } |
| 3785 | |
| 3786 | static void keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard, uint32_t serial, uint32_t mods_depressed, |
| 3787 | uint32_t mods_latched, uint32_t mods_locked, uint32_t group) {} |
| 3788 | |
| 3789 | static const struct wl_keyboard_listener keyboard_listener = { |
| 3790 | keyboard_handle_keymap, keyboard_handle_enter, keyboard_handle_leave, keyboard_handle_key, keyboard_handle_modifiers, |
| 3791 | }; |
| 3792 | |
| 3793 | static void seat_handle_capabilities(void *data, struct wl_seat *seat, enum wl_seat_capability caps) { |
| 3794 | // Subscribe to pointer events |
| 3795 | struct demo *demo = data; |
| 3796 | if ((caps & WL_SEAT_CAPABILITY_POINTER) && !demo->pointer) { |
| 3797 | demo->pointer = wl_seat_get_pointer(seat); |
| 3798 | wl_pointer_add_listener(demo->pointer, &pointer_listener, demo); |
| 3799 | } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && demo->pointer) { |
| 3800 | wl_pointer_destroy(demo->pointer); |
| 3801 | demo->pointer = NULL; |
| 3802 | } |
| 3803 | // Subscribe to keyboard events |
| 3804 | if (caps & WL_SEAT_CAPABILITY_KEYBOARD) { |
| 3805 | demo->keyboard = wl_seat_get_keyboard(seat); |
| 3806 | wl_keyboard_add_listener(demo->keyboard, &keyboard_listener, demo); |
| 3807 | } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD)) { |
| 3808 | wl_keyboard_destroy(demo->keyboard); |
| 3809 | demo->keyboard = NULL; |
| 3810 | } |
| 3811 | } |
| 3812 | |
| 3813 | static const struct wl_seat_listener seat_listener = { |
| 3814 | seat_handle_capabilities, |
| 3815 | }; |
| 3816 | |
Tony-LunarG | 5ceb7be | 2019-12-05 16:05:40 -0700 | [diff] [blame] | 3817 | static void registry_handle_global(void *data, struct wl_registry *registry, uint32_t id, const char *interface, uint32_t version) { |
Mun Gwan-gyeong | 6f01c55 | 2016-06-27 05:34:44 +0900 | [diff] [blame] | 3818 | struct demo *demo = data; |
Joey Bzdek | 15eb070 | 2017-06-07 09:40:36 -0600 | [diff] [blame] | 3819 | // pickup wayland objects when they appear |
Tony-LunarG | 5ceb7be | 2019-12-05 16:05:40 -0700 | [diff] [blame] | 3820 | if (strcmp(interface, "wl_compositor") == 0) { |
Georges Basile Stavracas Neto | 7da3ea7 | 2019-10-05 11:53:39 -0300 | [diff] [blame] | 3821 | uint32_t minVersion = version < 4 ? version : 4; |
| 3822 | demo->compositor = wl_registry_bind(registry, id, &wl_compositor_interface, minVersion); |
| 3823 | if (demo->VK_KHR_incremental_present_enabled && minVersion < 4) { |
| 3824 | fprintf(stderr, "Wayland compositor doesn't support VK_KHR_incremental_present, disabling.\n"); |
| 3825 | demo->VK_KHR_incremental_present_enabled = false; |
| 3826 | } |
Tony-LunarG | 5ceb7be | 2019-12-05 16:05:40 -0700 | [diff] [blame] | 3827 | } else if (strcmp(interface, "wl_shell") == 0) { |
| 3828 | demo->shell = wl_registry_bind(registry, id, &wl_shell_interface, 1); |
| 3829 | } else if (strcmp(interface, "wl_seat") == 0) { |
Joey Bzdek | 15eb070 | 2017-06-07 09:40:36 -0600 | [diff] [blame] | 3830 | demo->seat = wl_registry_bind(registry, id, &wl_seat_interface, 1); |
| 3831 | wl_seat_add_listener(demo->seat, &seat_listener, demo); |
Mun Gwan-gyeong | 6f01c55 | 2016-06-27 05:34:44 +0900 | [diff] [blame] | 3832 | } |
| 3833 | } |
| 3834 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 3835 | static void registry_handle_global_remove(void *data UNUSED, struct wl_registry *registry UNUSED, uint32_t name UNUSED) {} |
Mun Gwan-gyeong | 6f01c55 | 2016-06-27 05:34:44 +0900 | [diff] [blame] | 3836 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 3837 | static const struct wl_registry_listener registry_listener = {registry_handle_global, registry_handle_global_remove}; |
Mun Gwan-gyeong | 6f01c55 | 2016-06-27 05:34:44 +0900 | [diff] [blame] | 3838 | #endif |
| 3839 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 3840 | static void demo_init_connection(struct demo *demo) { |
Michael Lentine | c6cde4b | 2016-04-18 13:20:45 -0500 | [diff] [blame] | 3841 | #if defined(VK_USE_PLATFORM_XCB_KHR) |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 3842 | const xcb_setup_t *setup; |
| 3843 | xcb_screen_iterator_t iter; |
| 3844 | int scr; |
| 3845 | |
Mike Weiblen | 5d2ad54 | 2018-02-13 13:56:13 -0700 | [diff] [blame] | 3846 | const char *display_envar = getenv("DISPLAY"); |
| 3847 | if (display_envar == NULL || display_envar[0] == '\0') { |
| 3848 | printf("Environment variable DISPLAY requires a valid value.\nExiting ...\n"); |
| 3849 | fflush(stdout); |
| 3850 | exit(1); |
| 3851 | } |
| 3852 | |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 3853 | demo->connection = xcb_connect(NULL, &scr); |
Lenny Komow | 022bc2a | 2016-08-11 10:57:38 -0600 | [diff] [blame] | 3854 | if (xcb_connection_has_error(demo->connection) > 0) { |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 3855 | printf("Cannot find a compatible Vulkan installable client driver (ICD).\nExiting ...\n"); |
Ian Elliott | 3979e28 | 2015-04-03 15:24:55 -0600 | [diff] [blame] | 3856 | fflush(stdout); |
| 3857 | exit(1); |
| 3858 | } |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 3859 | |
| 3860 | setup = xcb_get_setup(demo->connection); |
| 3861 | iter = xcb_setup_roots_iterator(setup); |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 3862 | while (scr-- > 0) xcb_screen_next(&iter); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 3863 | |
| 3864 | demo->screen = iter.data; |
Mun Gwan-gyeong | 6f01c55 | 2016-06-27 05:34:44 +0900 | [diff] [blame] | 3865 | #elif defined(VK_USE_PLATFORM_WAYLAND_KHR) |
| 3866 | demo->display = wl_display_connect(NULL); |
| 3867 | |
| 3868 | if (demo->display == NULL) { |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 3869 | printf("Cannot find a compatible Vulkan installable client driver (ICD).\nExiting ...\n"); |
Mun Gwan-gyeong | 6f01c55 | 2016-06-27 05:34:44 +0900 | [diff] [blame] | 3870 | fflush(stdout); |
| 3871 | exit(1); |
| 3872 | } |
| 3873 | |
| 3874 | demo->registry = wl_display_get_registry(demo->display); |
| 3875 | wl_registry_add_listener(demo->registry, ®istry_listener, demo); |
| 3876 | wl_display_dispatch(demo->display); |
Michael Lentine | c6cde4b | 2016-04-18 13:20:45 -0500 | [diff] [blame] | 3877 | #endif |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 3878 | } |
| 3879 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 3880 | static void demo_init(struct demo *demo, int argc, char **argv) { |
Courtney Goeltzenleuchter | 8879d3a | 2014-10-29 08:29:35 -0600 | [diff] [blame] | 3881 | vec3 eye = {0.0f, 3.0f, 5.0f}; |
| 3882 | vec3 origin = {0, 0, 0}; |
Chia-I Wu | ae3b55d | 2015-04-22 14:56:17 +0800 | [diff] [blame] | 3883 | vec3 up = {0.0f, 1.0f, 0.0}; |
Courtney Goeltzenleuchter | 8879d3a | 2014-10-29 08:29:35 -0600 | [diff] [blame] | 3884 | |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 3885 | memset(demo, 0, sizeof(*demo)); |
Tony Barbour | 291d57b | 2016-10-21 14:47:07 -0600 | [diff] [blame] | 3886 | demo->presentMode = VK_PRESENT_MODE_FIFO_KHR; |
Tony Barbour | 1a86d03 | 2015-09-21 15:17:33 -0600 | [diff] [blame] | 3887 | demo->frameCount = INT32_MAX; |
Witold Baryluk | a3b988f | 2021-01-07 19:03:02 +0000 | [diff] [blame] | 3888 | /* Autodetect suitable / best GPU by default */ |
| 3889 | demo->gpu_number = -1; |
Nicolas Caramelli | 7c25ce9 | 2021-01-08 17:46:07 +0100 | [diff] [blame] | 3890 | demo->width = 500; |
| 3891 | demo->height = 500; |
Jeremy Kniager | 979b531 | 2019-11-22 14:55:57 -0700 | [diff] [blame] | 3892 | |
Piers Daniell | 735ee53 | 2015-02-23 16:23:13 -0700 | [diff] [blame] | 3893 | for (int i = 1; i < argc; i++) { |
Tony Barbour | 3dddd5d | 2015-04-29 16:19:20 -0600 | [diff] [blame] | 3894 | if (strcmp(argv[i], "--use_staging") == 0) { |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 3895 | demo->use_staging_buffer = true; |
Tony Barbour | 3dddd5d | 2015-04-29 16:19:20 -0600 | [diff] [blame] | 3896 | continue; |
Ian Elliott | 639ca47 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 3897 | } |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 3898 | if ((strcmp(argv[i], "--present_mode") == 0) && (i < argc - 1)) { |
| 3899 | demo->presentMode = atoi(argv[i + 1]); |
Tony Barbour | 291d57b | 2016-10-21 14:47:07 -0600 | [diff] [blame] | 3900 | i++; |
| 3901 | continue; |
| 3902 | } |
Courtney Goeltzenleuchter | 03f4b77 | 2015-07-22 11:03:51 -0600 | [diff] [blame] | 3903 | if (strcmp(argv[i], "--break") == 0) { |
| 3904 | demo->use_break = true; |
| 3905 | continue; |
| 3906 | } |
Tony Barbour | 3dddd5d | 2015-04-29 16:19:20 -0600 | [diff] [blame] | 3907 | if (strcmp(argv[i], "--validate") == 0) { |
| 3908 | demo->validate = true; |
| 3909 | continue; |
| 3910 | } |
Tobin Ehlis | 4eb29d6 | 2017-03-14 11:29:01 -0600 | [diff] [blame] | 3911 | if (strcmp(argv[i], "--validate-checks-disabled") == 0) { |
| 3912 | demo->validate = true; |
| 3913 | demo->validate_checks_disabled = true; |
| 3914 | continue; |
| 3915 | } |
Tony Barbour | 2593b18 | 2016-04-19 10:57:58 -0600 | [diff] [blame] | 3916 | if (strcmp(argv[i], "--xlib") == 0) { |
Tony Barbour | 153cb06 | 2016-12-07 13:43:36 -0700 | [diff] [blame] | 3917 | fprintf(stderr, "--xlib is deprecated and no longer does anything"); |
Tony Barbour | 2593b18 | 2016-04-19 10:57:58 -0600 | [diff] [blame] | 3918 | continue; |
| 3919 | } |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 3920 | if (strcmp(argv[i], "--c") == 0 && demo->frameCount == INT32_MAX && i < argc - 1 && |
| 3921 | sscanf(argv[i + 1], "%d", &demo->frameCount) == 1 && demo->frameCount >= 0) { |
David Pinedo | 2bb7c93 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 3922 | i++; |
| 3923 | continue; |
| 3924 | } |
Nicolas Caramelli | 7c25ce9 | 2021-01-08 17:46:07 +0100 | [diff] [blame] | 3925 | if (strcmp(argv[i], "--width") == 0 && i < argc - 1 && sscanf(argv[i + 1], "%d", &demo->width) == 1 && demo->width > 0) { |
| 3926 | i++; |
| 3927 | continue; |
| 3928 | } |
| 3929 | if (strcmp(argv[i], "--height") == 0 && i < argc - 1 && sscanf(argv[i + 1], "%d", &demo->height) == 1 && demo->height > 0) { |
| 3930 | i++; |
| 3931 | continue; |
| 3932 | } |
lenny-lunarg | fbe2239 | 2016-06-06 11:07:53 -0600 | [diff] [blame] | 3933 | if (strcmp(argv[i], "--suppress_popups") == 0) { |
| 3934 | demo->suppress_popups = true; |
| 3935 | continue; |
| 3936 | } |
Ian Elliott | d940ca2 | 2017-03-22 08:31:27 -0600 | [diff] [blame] | 3937 | if (strcmp(argv[i], "--display_timing") == 0) { |
| 3938 | demo->VK_GOOGLE_display_timing_enabled = true; |
| 3939 | continue; |
| 3940 | } |
Ian Elliott | 53622a7 | 2017-03-28 11:06:33 -0600 | [diff] [blame] | 3941 | if (strcmp(argv[i], "--incremental_present") == 0) { |
| 3942 | demo->VK_KHR_incremental_present_enabled = true; |
| 3943 | continue; |
| 3944 | } |
Tony-LunarG | 50e737c | 2020-07-16 14:26:03 -0600 | [diff] [blame] | 3945 | if ((strcmp(argv[i], "--gpu_number") == 0) && (i < argc - 1)) { |
| 3946 | demo->gpu_number = atoi(argv[i + 1]); |
Witold Baryluk | a3b988f | 2021-01-07 19:03:02 +0000 | [diff] [blame] | 3947 | assert(demo->gpu_number >= 0); |
Tony-LunarG | 50e737c | 2020-07-16 14:26:03 -0600 | [diff] [blame] | 3948 | i++; |
| 3949 | continue; |
| 3950 | } |
Tony Barbour | 3dddd5d | 2015-04-29 16:19:20 -0600 | [diff] [blame] | 3951 | |
Cody Northrop | af28a53 | 2016-09-27 10:50:57 -0600 | [diff] [blame] | 3952 | #if defined(ANDROID) |
Lenny Komow | ffe446c | 2018-11-19 17:08:04 -0700 | [diff] [blame] | 3953 | ERR_EXIT("Usage: vkcube [--validate]\n", "Usage"); |
Cody Northrop | af28a53 | 2016-09-27 10:50:57 -0600 | [diff] [blame] | 3954 | #else |
Tony-LunarG | d74734f | 2019-06-04 14:11:43 -0600 | [diff] [blame] | 3955 | char *message = |
| 3956 | "Usage:\n %s\t[--use_staging] [--validate] [--validate-checks-disabled]\n" |
| 3957 | "\t[--break] [--c <framecount>] [--suppress_popups]\n" |
| 3958 | "\t[--incremental_present] [--display_timing]\n" |
Tony-LunarG | 50e737c | 2020-07-16 14:26:03 -0600 | [diff] [blame] | 3959 | "\t[--gpu_number <index of physical device>]\n" |
Tony-LunarG | d74734f | 2019-06-04 14:11:43 -0600 | [diff] [blame] | 3960 | "\t[--present_mode <present mode enum>]\n" |
Nicolas Caramelli | 7c25ce9 | 2021-01-08 17:46:07 +0100 | [diff] [blame] | 3961 | "\t[--width <width>] [--height <height>]\n" |
Tony-LunarG | d74734f | 2019-06-04 14:11:43 -0600 | [diff] [blame] | 3962 | "\t<present_mode_enum>\n" |
| 3963 | "\t\tVK_PRESENT_MODE_IMMEDIATE_KHR = %d\n" |
| 3964 | "\t\tVK_PRESENT_MODE_MAILBOX_KHR = %d\n" |
| 3965 | "\t\tVK_PRESENT_MODE_FIFO_KHR = %d\n" |
| 3966 | "\t\tVK_PRESENT_MODE_FIFO_RELAXED_KHR = %d\n"; |
| 3967 | int length = snprintf(NULL, 0, message, APP_SHORT_NAME, VK_PRESENT_MODE_IMMEDIATE_KHR, VK_PRESENT_MODE_MAILBOX_KHR, |
| 3968 | VK_PRESENT_MODE_FIFO_KHR, VK_PRESENT_MODE_FIFO_RELAXED_KHR); |
| 3969 | char *usage = (char *)malloc(length + 1); |
| 3970 | if (!usage) { |
| 3971 | exit(1); |
| 3972 | } |
| 3973 | snprintf(usage, length + 1, message, APP_SHORT_NAME, VK_PRESENT_MODE_IMMEDIATE_KHR, VK_PRESENT_MODE_MAILBOX_KHR, |
| 3974 | VK_PRESENT_MODE_FIFO_KHR, VK_PRESENT_MODE_FIFO_RELAXED_KHR); |
| 3975 | #if defined(_WIN32) |
| 3976 | if (!demo->suppress_popups) MessageBox(NULL, usage, "Usage Error", MB_OK); |
| 3977 | #else |
Tony-LunarG | 122ac06 | 2019-06-13 16:21:57 -0600 | [diff] [blame] | 3978 | fprintf(stderr, "%s", usage); |
Ian Elliott | 639ca47 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 3979 | fflush(stderr); |
Tony-LunarG | d74734f | 2019-06-04 14:11:43 -0600 | [diff] [blame] | 3980 | #endif |
| 3981 | free(usage); |
Ian Elliott | 639ca47 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 3982 | exit(1); |
Cody Northrop | af28a53 | 2016-09-27 10:50:57 -0600 | [diff] [blame] | 3983 | #endif |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 3984 | } |
| 3985 | |
Tony Barbour | 153cb06 | 2016-12-07 13:43:36 -0700 | [diff] [blame] | 3986 | demo_init_connection(demo); |
Tony Barbour | 2593b18 | 2016-04-19 10:57:58 -0600 | [diff] [blame] | 3987 | |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 3988 | demo_init_vk(demo); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 3989 | |
Tony Barbour | 66a56c3 | 2016-09-21 13:10:56 -0600 | [diff] [blame] | 3990 | demo->spin_angle = 4.0f; |
| 3991 | demo->spin_increment = 0.2f; |
Courtney Goeltzenleuchter | 8879d3a | 2014-10-29 08:29:35 -0600 | [diff] [blame] | 3992 | demo->pause = false; |
| 3993 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 3994 | mat4x4_perspective(demo->projection_matrix, (float)degreesToRadians(45.0f), 1.0f, 0.1f, 100.0f); |
Courtney Goeltzenleuchter | 8879d3a | 2014-10-29 08:29:35 -0600 | [diff] [blame] | 3995 | mat4x4_look_at(demo->view_matrix, eye, origin, up); |
| 3996 | mat4x4_identity(demo->model_matrix); |
Rene Lindsay | bcccdea | 2016-08-12 17:56:09 -0600 | [diff] [blame] | 3997 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 3998 | demo->projection_matrix[1][1] *= -1; // Flip projection matrix from GL to Vulkan orientation. |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 3999 | } |
| 4000 | |
Michael Lentine | c6cde4b | 2016-04-18 13:20:45 -0500 | [diff] [blame] | 4001 | #if defined(VK_USE_PLATFORM_WIN32_KHR) |
Mark Young | 418b9d1 | 2016-01-06 14:26:04 -0700 | [diff] [blame] | 4002 | // Include header required for parsing the command line options. |
| 4003 | #include <shellapi.h> |
Ian Elliott | f9cf78c | 2015-04-28 10:33:11 -0600 | [diff] [blame] | 4004 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 4005 | int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pCmdLine, int nCmdShow) { |
| 4006 | MSG msg; // message |
| 4007 | bool done; // flag saying when app is complete |
Tony Barbour | 3dddd5d | 2015-04-29 16:19:20 -0600 | [diff] [blame] | 4008 | int argc; |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 4009 | char **argv; |
Ian Elliott | 639ca47 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 4010 | |
Jamie Madill | b2ff650 | 2017-03-15 16:17:46 -0400 | [diff] [blame] | 4011 | // Ensure wParam is initialized. |
| 4012 | msg.wParam = 0; |
| 4013 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 4014 | // Use the CommandLine functions to get the command line arguments. |
| 4015 | // Unfortunately, Microsoft outputs |
| 4016 | // this information as wide characters for Unicode, and we simply want the |
| 4017 | // Ascii version to be compatible |
| 4018 | // with the non-Windows side. So, we have to convert the information to |
| 4019 | // Ascii character strings. |
| 4020 | LPWSTR *commandLineArgs = CommandLineToArgvW(GetCommandLineW(), &argc); |
| 4021 | if (NULL == commandLineArgs) { |
Mark Young | 418b9d1 | 2016-01-06 14:26:04 -0700 | [diff] [blame] | 4022 | argc = 0; |
| 4023 | } |
| 4024 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 4025 | if (argc > 0) { |
| 4026 | argv = (char **)malloc(sizeof(char *) * argc); |
| 4027 | if (argv == NULL) { |
Mark Young | 418b9d1 | 2016-01-06 14:26:04 -0700 | [diff] [blame] | 4028 | argc = 0; |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 4029 | } else { |
| 4030 | for (int iii = 0; iii < argc; iii++) { |
| 4031 | size_t wideCharLen = wcslen(commandLineArgs[iii]); |
Mark Young | 418b9d1 | 2016-01-06 14:26:04 -0700 | [diff] [blame] | 4032 | size_t numConverted = 0; |
| 4033 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 4034 | argv[iii] = (char *)malloc(sizeof(char) * (wideCharLen + 1)); |
| 4035 | if (argv[iii] != NULL) { |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 4036 | wcstombs_s(&numConverted, argv[iii], wideCharLen + 1, commandLineArgs[iii], wideCharLen + 1); |
Mark Young | 418b9d1 | 2016-01-06 14:26:04 -0700 | [diff] [blame] | 4037 | } |
| 4038 | } |
| 4039 | } |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 4040 | } else { |
Mark Young | 418b9d1 | 2016-01-06 14:26:04 -0700 | [diff] [blame] | 4041 | argv = NULL; |
| 4042 | } |
Tony Barbour | 3dddd5d | 2015-04-29 16:19:20 -0600 | [diff] [blame] | 4043 | |
| 4044 | demo_init(&demo, argc, argv); |
Mark Young | 418b9d1 | 2016-01-06 14:26:04 -0700 | [diff] [blame] | 4045 | |
| 4046 | // Free up the items we had to allocate for the command line arguments. |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 4047 | if (argc > 0 && argv != NULL) { |
| 4048 | for (int iii = 0; iii < argc; iii++) { |
| 4049 | if (argv[iii] != NULL) { |
Mark Young | 418b9d1 | 2016-01-06 14:26:04 -0700 | [diff] [blame] | 4050 | free(argv[iii]); |
| 4051 | } |
| 4052 | } |
| 4053 | free(argv); |
| 4054 | } |
| 4055 | |
Tony Barbour | 3dddd5d | 2015-04-29 16:19:20 -0600 | [diff] [blame] | 4056 | demo.connection = hInstance; |
Lenny Komow | ffe446c | 2018-11-19 17:08:04 -0700 | [diff] [blame] | 4057 | strncpy(demo.name, "Vulkan Cube", APP_NAME_STR_LEN); |
Ian Elliott | 639ca47 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 4058 | demo_create_window(&demo); |
Tony Barbour | cc69f45 | 2015-10-08 13:59:42 -0600 | [diff] [blame] | 4059 | demo_init_vk_swapchain(&demo); |
Ian Elliott | 639ca47 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 4060 | |
| 4061 | demo_prepare(&demo); |
| 4062 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 4063 | done = false; // initialize loop condition variable |
Mark Young | 418b9d1 | 2016-01-06 14:26:04 -0700 | [diff] [blame] | 4064 | |
| 4065 | // main message loop |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 4066 | while (!done) { |
Petr Kraus | d88e4e5 | 2019-01-23 18:45:04 +0100 | [diff] [blame] | 4067 | if (demo.pause) { |
| 4068 | const BOOL succ = WaitMessage(); |
| 4069 | |
| 4070 | if (!succ) { |
| 4071 | struct demo *tmp = &demo; |
| 4072 | struct demo *demo = tmp; |
| 4073 | ERR_EXIT("WaitMessage() failed on paused demo", "event loop error"); |
| 4074 | } |
| 4075 | } |
Ian Elliott | a748eaf | 2015-04-28 15:50:36 -0600 | [diff] [blame] | 4076 | PeekMessage(&msg, NULL, 0, 0, PM_REMOVE); |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 4077 | if (msg.message == WM_QUIT) // check for a quit message |
Ian Elliott | 639ca47 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 4078 | { |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 4079 | done = true; // if found, quit app |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 4080 | } else { |
Ian Elliott | 639ca47 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 4081 | /* Translate and dispatch to event queue*/ |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 4082 | TranslateMessage(&msg); |
Ian Elliott | 639ca47 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 4083 | DispatchMessage(&msg); |
| 4084 | } |
Tony Barbour | c8a5989 | 2015-10-20 12:49:46 -0600 | [diff] [blame] | 4085 | RedrawWindow(demo.window, NULL, NULL, RDW_INTERNALPAINT); |
Ian Elliott | 639ca47 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 4086 | } |
| 4087 | |
| 4088 | demo_cleanup(&demo); |
| 4089 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 4090 | return (int)msg.wParam; |
Ian Elliott | 639ca47 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 4091 | } |
Bill Hollings | f435214 | 2017-02-14 22:58:56 -0500 | [diff] [blame] | 4092 | |
Bill Hollings | 0a0625a | 2019-07-15 17:39:18 -0400 | [diff] [blame] | 4093 | #elif defined(VK_USE_PLATFORM_METAL_EXT) |
| 4094 | static void demo_main(struct demo *demo, void *caMetalLayer, int argc, const char *argv[]) { |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 4095 | demo_init(demo, argc, (char **)argv); |
Bill Hollings | 0a0625a | 2019-07-15 17:39:18 -0400 | [diff] [blame] | 4096 | demo->caMetalLayer = caMetalLayer; |
Tony Barbour | c65cd75 | 2017-03-13 15:29:35 -0600 | [diff] [blame] | 4097 | demo_init_vk_swapchain(demo); |
| 4098 | demo_prepare(demo); |
| 4099 | demo->spin_angle = 0.4f; |
Bill Hollings | f435214 | 2017-02-14 22:58:56 -0500 | [diff] [blame] | 4100 | } |
| 4101 | |
Michael Lentine | c6cde4b | 2016-04-18 13:20:45 -0500 | [diff] [blame] | 4102 | #elif defined(VK_USE_PLATFORM_ANDROID_KHR) |
| 4103 | #include <android/log.h> |
| 4104 | #include <android_native_app_glue.h> |
Cody Northrop | c5e5a8c | 2016-09-26 21:05:00 -0600 | [diff] [blame] | 4105 | #include "android_util.h" |
| 4106 | |
Michael Lentine | c6cde4b | 2016-04-18 13:20:45 -0500 | [diff] [blame] | 4107 | static bool initialized = false; |
| 4108 | static bool active = false; |
| 4109 | struct demo demo; |
| 4110 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 4111 | static int32_t processInput(struct android_app *app, AInputEvent *event) { return 0; } |
Michael Lentine | c6cde4b | 2016-04-18 13:20:45 -0500 | [diff] [blame] | 4112 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 4113 | static void processCommand(struct android_app *app, int32_t cmd) { |
| 4114 | switch (cmd) { |
Michael Lentine | c6cde4b | 2016-04-18 13:20:45 -0500 | [diff] [blame] | 4115 | case APP_CMD_INIT_WINDOW: { |
| 4116 | if (app->window) { |
Ian Elliott | f05d5d1 | 2016-05-17 12:03:35 -0600 | [diff] [blame] | 4117 | // We're getting a new window. If the app is starting up, we |
| 4118 | // need to initialize. If the app has already been |
| 4119 | // initialized, that means that we lost our previous window, |
| 4120 | // which means that we have a lot of work to do. At a minimum, |
| 4121 | // we need to destroy the swapchain and surface associated with |
| 4122 | // the old window, and create a new surface and swapchain. |
| 4123 | // However, since there are a lot of other objects/state that |
| 4124 | // is tied to the swapchain, it's easiest to simply cleanup and |
| 4125 | // start over (i.e. use a brute-force approach of re-starting |
| 4126 | // the app) |
| 4127 | if (demo.prepared) { |
| 4128 | demo_cleanup(&demo); |
| 4129 | } |
Cody Northrop | c5e5a8c | 2016-09-26 21:05:00 -0600 | [diff] [blame] | 4130 | |
| 4131 | // Parse Intents into argc, argv |
| 4132 | // Use the following key to send arguments, i.e. |
| 4133 | // --es args "--validate" |
| 4134 | const char key[] = "args"; |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 4135 | char *appTag = (char *)APP_SHORT_NAME; |
Cody Northrop | c5e5a8c | 2016-09-26 21:05:00 -0600 | [diff] [blame] | 4136 | int argc = 0; |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 4137 | char **argv = get_args(app, key, appTag, &argc); |
Cody Northrop | c5e5a8c | 2016-09-26 21:05:00 -0600 | [diff] [blame] | 4138 | |
| 4139 | __android_log_print(ANDROID_LOG_INFO, appTag, "argc = %i", argc); |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 4140 | for (int i = 0; i < argc; i++) __android_log_print(ANDROID_LOG_INFO, appTag, "argv[%i] = %s", i, argv[i]); |
Cody Northrop | c5e5a8c | 2016-09-26 21:05:00 -0600 | [diff] [blame] | 4141 | |
| 4142 | demo_init(&demo, argc, argv); |
| 4143 | |
| 4144 | // Free the argv malloc'd by get_args |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 4145 | for (int i = 0; i < argc; i++) free(argv[i]); |
Cody Northrop | c5e5a8c | 2016-09-26 21:05:00 -0600 | [diff] [blame] | 4146 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 4147 | demo.window = (void *)app->window; |
Michael Lentine | c6cde4b | 2016-04-18 13:20:45 -0500 | [diff] [blame] | 4148 | demo_init_vk_swapchain(&demo); |
| 4149 | demo_prepare(&demo); |
| 4150 | initialized = true; |
| 4151 | } |
| 4152 | break; |
| 4153 | } |
| 4154 | case APP_CMD_GAINED_FOCUS: { |
| 4155 | active = true; |
| 4156 | break; |
| 4157 | } |
| 4158 | case APP_CMD_LOST_FOCUS: { |
| 4159 | active = false; |
| 4160 | break; |
| 4161 | } |
| 4162 | } |
| 4163 | } |
| 4164 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 4165 | void android_main(struct android_app *app) { |
Cody Northrop | 8707a6e | 2016-04-26 19:59:19 -0600 | [diff] [blame] | 4166 | #ifdef ANDROID |
| 4167 | int vulkanSupport = InitVulkan(); |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 4168 | if (vulkanSupport == 0) return; |
Cody Northrop | 8707a6e | 2016-04-26 19:59:19 -0600 | [diff] [blame] | 4169 | #endif |
| 4170 | |
Ian Elliott | f05d5d1 | 2016-05-17 12:03:35 -0600 | [diff] [blame] | 4171 | demo.prepared = false; |
| 4172 | |
Michael Lentine | c6cde4b | 2016-04-18 13:20:45 -0500 | [diff] [blame] | 4173 | app->onAppCmd = processCommand; |
| 4174 | app->onInputEvent = processInput; |
| 4175 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 4176 | while (1) { |
Michael Lentine | c6cde4b | 2016-04-18 13:20:45 -0500 | [diff] [blame] | 4177 | int events; |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 4178 | struct android_poll_source *source; |
| 4179 | while (ALooper_pollAll(active ? 0 : -1, NULL, &events, (void **)&source) >= 0) { |
Michael Lentine | c6cde4b | 2016-04-18 13:20:45 -0500 | [diff] [blame] | 4180 | if (source) { |
| 4181 | source->process(app, source); |
| 4182 | } |
| 4183 | |
| 4184 | if (app->destroyRequested != 0) { |
| 4185 | demo_cleanup(&demo); |
| 4186 | return; |
| 4187 | } |
| 4188 | } |
| 4189 | if (initialized && active) { |
| 4190 | demo_run(&demo); |
| 4191 | } |
| 4192 | } |
Michael Lentine | c6cde4b | 2016-04-18 13:20:45 -0500 | [diff] [blame] | 4193 | } |
Tobin Ehlis | 43ed298 | 2016-04-28 10:17:33 -0600 | [diff] [blame] | 4194 | #else |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 4195 | int main(int argc, char **argv) { |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 4196 | struct demo demo; |
| 4197 | |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 4198 | demo_init(&demo, argc, argv); |
Tony Barbour | 153cb06 | 2016-12-07 13:43:36 -0700 | [diff] [blame] | 4199 | #if defined(VK_USE_PLATFORM_XCB_KHR) |
Tony Barbour | 8295ac4 | 2016-08-08 11:04:17 -0600 | [diff] [blame] | 4200 | demo_create_xcb_window(&demo); |
| 4201 | #elif defined(VK_USE_PLATFORM_XLIB_KHR) |
| 4202 | demo_create_xlib_window(&demo); |
Mun Gwan-gyeong | 6f01c55 | 2016-06-27 05:34:44 +0900 | [diff] [blame] | 4203 | #elif defined(VK_USE_PLATFORM_WAYLAND_KHR) |
| 4204 | demo_create_window(&demo); |
Nicolas Caramelli | c8be8c8 | 2020-07-13 17:22:09 +0200 | [diff] [blame] | 4205 | #elif defined(VK_USE_PLATFORM_DIRECTFB_EXT) |
| 4206 | demo_create_directfb_window(&demo); |
Mun Gwan-gyeong | 6f01c55 | 2016-06-27 05:34:44 +0900 | [diff] [blame] | 4207 | #endif |
Tony Barbour | 2593b18 | 2016-04-19 10:57:58 -0600 | [diff] [blame] | 4208 | |
Tony Barbour | cc69f45 | 2015-10-08 13:59:42 -0600 | [diff] [blame] | 4209 | demo_init_vk_swapchain(&demo); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 4210 | |
| 4211 | demo_prepare(&demo); |
Tony Barbour | 2593b18 | 2016-04-19 10:57:58 -0600 | [diff] [blame] | 4212 | |
Tony Barbour | 153cb06 | 2016-12-07 13:43:36 -0700 | [diff] [blame] | 4213 | #if defined(VK_USE_PLATFORM_XCB_KHR) |
Tony Barbour | 8295ac4 | 2016-08-08 11:04:17 -0600 | [diff] [blame] | 4214 | demo_run_xcb(&demo); |
| 4215 | #elif defined(VK_USE_PLATFORM_XLIB_KHR) |
| 4216 | demo_run_xlib(&demo); |
Mun Gwan-gyeong | 6f01c55 | 2016-06-27 05:34:44 +0900 | [diff] [blame] | 4217 | #elif defined(VK_USE_PLATFORM_WAYLAND_KHR) |
| 4218 | demo_run(&demo); |
Nicolas Caramelli | c8be8c8 | 2020-07-13 17:22:09 +0200 | [diff] [blame] | 4219 | #elif defined(VK_USE_PLATFORM_DIRECTFB_EXT) |
| 4220 | demo_run_directfb(&demo); |
Damien Leone | 600c305 | 2017-01-31 10:26:07 -0700 | [diff] [blame] | 4221 | #elif defined(VK_USE_PLATFORM_DISPLAY_KHR) |
| 4222 | demo_run_display(&demo); |
Mun Gwan-gyeong | 6f01c55 | 2016-06-27 05:34:44 +0900 | [diff] [blame] | 4223 | #endif |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 4224 | |
| 4225 | demo_cleanup(&demo); |
| 4226 | |
Karl Schultz | 0218c00 | 2016-03-22 17:06:13 -0600 | [diff] [blame] | 4227 | return validation_error; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 4228 | } |
Michael Lentine | c6cde4b | 2016-04-18 13:20:45 -0500 | [diff] [blame] | 4229 | #endif |