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